forked from zxcalc/pyzx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlong_scalar_test.py
90 lines (72 loc) · 3.08 KB
/
long_scalar_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# PyZX - Python library for quantum circuit rewriting
# and optimization using the ZX-calculus
# Copyright (C) 2018 - Aleks Kissinger and John van de Wetering
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import random
import sys
if __name__ == '__main__':
sys.path.append('..')
sys.path.append('.')
import numpy as np
from pyzx.tensor import compare_tensors
from pyzx.generate import CNOT_HAD_PHASE_circuit
from pyzx.simplify import clifford_simp, full_reduce, reduce_scalar
from pyzx.simulate import calculate_path_sum
from pyzx.circuit import Circuit
SEED = 1337
random.seed(SEED)
def compare(a,b):
try:
if not compare_tensors(a, b):
raise AssertionError("Not equal")
except (ValueError, MemoryError): pass
def do_tests(qubits, depth, iterations):
print("Starting test with circuits of {:d} qubits and {:d} depth. {:d} iterations".format(qubits, depth, iterations))
try:
for i in range(1, iterations+1):
if i%25 == 0: print(i, end='.', flush=True)
seed = random.randint(100000,500000)
random.seed(seed)
c = CNOT_HAD_PHASE_circuit(qubits,depth,p_had=0.1, p_t=0.3)
g = c.to_graph()
g.apply_state(''.join(random.choice('+-01') for _ in range(qubits)))
g.apply_effect(''.join(random.choice('+-01') for _ in range(qubits)))
t = g.to_tensor()
g2 = g.copy()
clifford_simp(g2,quiet=True)
steps = ["clifford_simp"]
compare(t, g2)
g2 = g.copy()
full_reduce(g2,quiet=True)
steps = ["full_reduce"]
compare(t, g2)
val = calculate_path_sum(g2)
steps.append("calculate_path_sum")
compare(t,np.array([val]))
g2 = g.copy()
steps = ["clifford_simp", "reduce_scalar"]
clifford_simp(g2,quiet=True)
reduce_scalar(g2,quiet=True)
compare(t,g2)
except AssertionError:
print("Unequality for circuit with seed {:d}, qubits {:d} and depth {:d}".format(seed, qubits, depth))
print("It went wrong at step {} with total sequence {}".format(steps[-1],str(steps)))
except Exception as e:
print("An exception occured for circuit with seed {:d}, qubits {:d} and depth {:d}".format(seed, qubits, depth))
print("It went wrong at step {} with total sequence {}".format(steps[-1],str(steps)))
raise e
else:
print("\nTests finished successfully")
do_tests(3, 20, 500)
do_tests(3, 50, 500)
do_tests(4,70,500)
do_tests(5, 100, 250)
do_tests(10, 100, 250)