Browse Source

Fix dumb errors

master
Pete Shadbolt 8 years ago
parent
commit
171079f98e
2 changed files with 32 additions and 12 deletions
  1. +14
    -5
      abp/qi.py
  2. +18
    -7
      tests/test_qi_circuit_model.py

+ 14
- 5
abp/qi.py View File

@@ -59,7 +59,6 @@ class CircuitModel(object):
""" Act a CU somewhere """ """ Act a CU somewhere """
control = 1 << control control = 1 << control
target = 1 << control target = 1 << control
print control, target
for i in xrange(self.d): for i in xrange(self.d):
if (i & control) and (i & target): if (i & control) and (i & target):
self.state[i, 0] *= -1 self.state[i, 0] *= -1
@@ -73,14 +72,24 @@ class CircuitModel(object):
output[i] += v*ir2 output[i] += v*ir2
output[i ^ where] += v*ir2 output[i ^ where] += v*ir2
if (i & where) == 1: if (i & where) == 1:
output[i] += v*ir2
output[i ^ where] -= v*ir2
output[i ^ where] += v*ir2
output[i] -= v*ir2
self.state = output self.state = output




def local_rotation(self, qubit, u):
def act_local_rotation(self, qubit, u):
""" Act a local unitary somwhere """ """ Act a local unitary somwhere """
pass
where = 1 << qubit
output = np.zeros((self.d, 1), dtype=complex)
for i, v in enumerate(self.state):
if (i & where) == 0:
output[i] += v*u[0, 0]
output[i ^ where] += v*u[0, 1]
if (i & where) == 1:
output[i ^ where] += v*u[1, 0]
output[i] += v*u[1, 1]
self.state = output



def __str__(self): def __str__(self):
s = "" s = ""


+ 18
- 7
tests/test_qi_circuit_model.py View File

@@ -1,3 +1,4 @@
import numpy as np
from abp import qi from abp import qi


def test_init(): def test_init():
@@ -5,18 +6,28 @@ def test_init():
psi = qi.CircuitModel(5) psi = qi.CircuitModel(5)
assert psi.d == 32 assert psi.d == 32


def test_hadamard():
""" What does CZ do ? """
psi = qi.CircuitModel(10)
psi.act_hadamard(0)
psi.act_hadamard(0)
assert np.allclose(psi.state[0], 1)


def test_cz(): def test_cz():
""" What does CZ do ? """ """ What does CZ do ? """
psi = qi.CircuitModel(2) psi = qi.CircuitModel(2)
#psi.act_hadamard(0)
psi.act_hadamard(0) psi.act_hadamard(0)
print psi
psi.act_hadamard(1) psi.act_hadamard(1)
print psi
psi.act_cz(0, 1) psi.act_cz(0, 1)
print psi
psi.act_cz(0, 1)
print psi
#psi.act_cz(0, 1)
assert np.allclose(psi.state, qi.bond)


def test_local_rotation():
""" Do local rotations work okay? ? """
psi = qi.CircuitModel(2)
psi.act_local_rotation(0, qi.ha)
psi.act_local_rotation(0, qi.ha)
assert np.allclose(psi.state[0], 1)





Loading…
Cancel
Save