Sfoglia il codice sorgente

Fix dumb errors

master
Pete Shadbolt 8 anni fa
parent
commit
171079f98e
2 ha cambiato i file con 32 aggiunte e 12 eliminazioni
  1. +14
    -5
      abp/qi.py
  2. +18
    -7
      tests/test_qi_circuit_model.py

+ 14
- 5
abp/qi.py Vedi File

@@ -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 = ""


+ 18
- 7
tests/test_qi_circuit_model.py Vedi File

@@ -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)



Loading…
Annulla
Salva