From 88b99cc22a59f49f5bb78be03760d335b383e7eb Mon Sep 17 00:00:00 2001 From: Pete Shadbolt Date: Mon, 25 Jul 2016 02:50:24 +0100 Subject: [PATCH] Test passing, still need to do the nightmare --- tests/test_graphstate.py | 6 ++--- tests/test_qi.py | 53 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/tests/test_graphstate.py b/tests/test_graphstate.py index a5741c4..b63f053 100644 --- a/tests/test_graphstate.py +++ b/tests/test_graphstate.py @@ -96,7 +96,7 @@ def test_local_complementation(): def test_single_qubit(): """ A multi qubit test with Hadamards only""" - for repeat in tqdm(range(REPEATS), desc="Randomly testing single qubit operations against circuit model"): + for repeat in tqdm(range(REPEATS), desc="Single qubit rotations against CircuitModel"): circuit = [(0, random.choice(range(24))) for i in range(DEPTH)] a = mock.circuit_to_state(mock.ABPWrapper, 1, circuit) b = mock.circuit_to_state(mock.CircuitModelWrapper, 1, circuit) @@ -105,7 +105,7 @@ def test_single_qubit(): def test_graph_state_multiqubit(n=6): """ A multi qubit test with Hadamards only""" - for repeat in tqdm(range(REPEATS), desc="Random graph states against the circuit model"): + for repeat in tqdm(range(REPEATS), desc="Random graph states against the CircuitModel"): circuit = mock.random_graph_circuit(n) a = mock.circuit_to_state(mock.ABPWrapper, n, circuit) b = mock.circuit_to_state(mock.CircuitModelWrapper, n, circuit) @@ -114,7 +114,7 @@ def test_graph_state_multiqubit(n=6): def test_stabilizer_state_multiqubit(n=6): """ A multi qubit test with arbitrary local rotations """ - for repeat in tqdm(range(REPEATS), desc="Random Clifford circuits against the circuit model"): + for repeat in tqdm(range(REPEATS), desc="Random Clifford circuits against the CircuitModel"): circuit = mock.random_stabilizer_circuit(n) a = mock.circuit_to_state(mock.ABPWrapper, n, circuit) b = mock.circuit_to_state(mock.CircuitModelWrapper, n, circuit) diff --git a/tests/test_qi.py b/tests/test_qi.py index 7a28f74..5f167c2 100644 --- a/tests/test_qi.py +++ b/tests/test_qi.py @@ -1,7 +1,8 @@ import numpy as np -from abp import qi -from abp import GraphState +from abp import qi, GraphState +from tqdm import tqdm +DEPTH = 1000 def test_init(): """ Can you initialize some qubits """ @@ -108,11 +109,55 @@ def test_to_state_vector_single_qubit(): g.act_cz(0, 1) assert np.allclose(g.to_state_vector().state, qi.bond) + def test_normalize_global_phase(): """ We should be able to see that two states are equivalent up to a global phase """ for i in range(10): u = qi.pz - phase = np.random.uniform(0, 2*np.pi) - m = np.exp(1j*phase) * u + phase = np.random.uniform(0, 2 * np.pi) + m = np.exp(1j * phase) * u normalized = qi.normalize_global_phase(m) assert np.allclose(normalized, u) + + +def test_against_chp(n=5): + """ Test against CHP if it is installed """ + try: + import chp + except ImportError: + print "Not testing against CHP -- not installed" + return + + def get_chp_state(): + """ Helper to convert CHP to CircuitModel """ + output = qi.CircuitModel(n) + ket = chp.get_ket() + nonzero = np.sqrt(len(ket)) + output.state[0, 0] = 0 + for key, phase in ket.items(): + output.state[key] = np.exp(1j * phase * np.pi / 2) / nonzero + return output + + # Run a simple circuit + chp.init(n) + chp.act_hadamard(0) + chp.act_cnot(0, 1) + psi = qi.CircuitModel(n) + psi.act_hadamard(0) + psi.act_cnot(0, 1) + assert psi == get_chp_state() + + # Run a random circuit + chp.init(n) + psi = qi.CircuitModel(n) + for i in tqdm(range(DEPTH), "Testing CircuitModel against CHP"): + if np.random.rand() > .5: + a = np.random.randint(0, n - 1) + chp.act_hadamard(a) + psi.act_hadamard(a) + else: + a, b = np.random.randint(0, n - 1, 2) + if a != b: + chp.act_cnot(a, b) + psi.act_cnot(a, b) + assert psi == get_chp_state()