diff --git a/abp/qi.py b/abp/qi.py index 14f64b1..e942995 100644 --- a/abp/qi.py +++ b/abp/qi.py @@ -59,7 +59,6 @@ class CircuitModel(object): """ Act a CU somewhere """ control = 1 << control target = 1 << control - print control, target for i in xrange(self.d): if (i & control) and (i & target): self.state[i, 0] *= -1 @@ -73,14 +72,24 @@ class CircuitModel(object): output[i] += v*ir2 output[i ^ where] += v*ir2 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 - def local_rotation(self, qubit, u): + def act_local_rotation(self, qubit, u): """ 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): s = "" diff --git a/tests/test_qi_circuit_model.py b/tests/test_qi_circuit_model.py index 1371e0b..19c0ec4 100644 --- a/tests/test_qi_circuit_model.py +++ b/tests/test_qi_circuit_model.py @@ -1,3 +1,4 @@ +import numpy as np from abp import qi def test_init(): @@ -5,18 +6,28 @@ def test_init(): psi = qi.CircuitModel(5) 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(): """ What does CZ do ? """ psi = qi.CircuitModel(2) - #psi.act_hadamard(0) psi.act_hadamard(0) - print psi psi.act_hadamard(1) - print psi 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)