Skip to content

Commit

Permalink
Fix Q# test - assert up to global phase
Browse files Browse the repository at this point in the history
  • Loading branch information
fedimser committed Jan 17, 2025
1 parent 353947c commit 31bb37d
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions quantum_decomp/qsharp_integration_test.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
"""
This test verifies that Q# code generated by this library actually compiles to
an operation, which applies specified matrix to a register of qubits.
Here we just test just few small random matrices. More thorough tests are in
quantum_decomp_test.py.
"""

from contextlib import redirect_stdout
import io
import tempfile
from contextlib import redirect_stdout

import cirq
import numpy as np
import qsharp
import io
from scipy.stats import unitary_group

import quantum_decomp as qd
from quantum_decomp.src.test_utils import QFT_2, SWAP, CNOT
from quantum_decomp.src.test_utils import CNOT, QFT_2, SWAP
from quantum_decomp.src.utils import permute_matrix


def change_int_endianness(val, n):
return sum(((val >> i) & 1) << (n-1-i) for i in range(n))
return sum(((val >> i) & 1) << (n - 1 - i) for i in range(n))


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


def test_qsharp_integration_2x2():
check_on_matrix(unitary_group.rvs(2))
check_on_matrix(unitary_group.rvs(2))
for _ in range(10):
check_on_matrix(unitary_group.rvs(2))


def test_qsharp_integration_4x4():
Expand All @@ -75,3 +74,12 @@ def test_qsharp_integration_4x4():
check_on_matrix(QFT_2)
for _ in range(10):
check_on_matrix(unitary_group.rvs(4))


def test_qsharp_integration_8x8():
for _ in range(10):
check_on_matrix(unitary_group.rvs(8))


def test_qsharp_integration_16x16():
check_on_matrix(unitary_group.rvs(16))

0 comments on commit 31bb37d

Please sign in to comment.