Skip to content

Commit 31bb37d

Browse files
committed
Fix Q# test - assert up to global phase
1 parent 353947c commit 31bb37d

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

quantum_decomp/qsharp_integration_test.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
"""
22
This test verifies that Q# code generated by this library actually compiles to
33
an operation, which applies specified matrix to a register of qubits.
4-
5-
Here we just test just few small random matrices. More thorough tests are in
6-
quantum_decomp_test.py.
74
"""
85

9-
from contextlib import redirect_stdout
6+
import io
107
import tempfile
8+
from contextlib import redirect_stdout
9+
10+
import cirq
1111
import numpy as np
1212
import qsharp
13-
import io
1413
from scipy.stats import unitary_group
1514

1615
import quantum_decomp as qd
17-
from quantum_decomp.src.test_utils import QFT_2, SWAP, CNOT
16+
from quantum_decomp.src.test_utils import CNOT, QFT_2, SWAP
1817
from quantum_decomp.src.utils import permute_matrix
1918

2019

2120
def change_int_endianness(val, n):
22-
return sum(((val >> i) & 1) << (n-1-i) for i in range(n))
21+
return sum(((val >> i) & 1) << (n - 1 - i) for i in range(n))
2322

2423

2524
def change_matrix_endianness(matrix, n):
@@ -61,12 +60,12 @@ def check_on_matrix(matrix):
6160
op_code = qd.matrix_to_qsharp(matrix, op_name='Op')
6261
qubits_count = int(np.log2(matrix.shape[0]))
6362
dump_matrix = dump_qsharp_unitary(op_code, qubits_count)
64-
assert np.allclose(matrix, dump_matrix, atol=1e-4)
63+
assert cirq.equal_up_to_global_phase(matrix, dump_matrix, atol=1e-4)
6564

6665

6766
def test_qsharp_integration_2x2():
68-
check_on_matrix(unitary_group.rvs(2))
69-
check_on_matrix(unitary_group.rvs(2))
67+
for _ in range(10):
68+
check_on_matrix(unitary_group.rvs(2))
7069

7170

7271
def test_qsharp_integration_4x4():
@@ -75,3 +74,12 @@ def test_qsharp_integration_4x4():
7574
check_on_matrix(QFT_2)
7675
for _ in range(10):
7776
check_on_matrix(unitary_group.rvs(4))
77+
78+
79+
def test_qsharp_integration_8x8():
80+
for _ in range(10):
81+
check_on_matrix(unitary_group.rvs(8))
82+
83+
84+
def test_qsharp_integration_16x16():
85+
check_on_matrix(unitary_group.rvs(16))

0 commit comments

Comments
 (0)