Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilising test_squ #9083

Merged
merged 3 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion qiskit/extensions/quantum_initializer/squ.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def squ(
"""Decompose an arbitrary 2*2 unitary into three rotation gates.

Note that the decomposition is up to a global phase shift.
(This is a well known decomposition, which can be found for example in Nielsen and Chuang's book
(This is a well known decomposition which can be found for example in Nielsen and Chuang's book
"Quantum computation and quantum information".)

Args:
Expand Down
39 changes: 20 additions & 19 deletions test/python/circuit/test_squ.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

"""Single-qubit unitary tests."""

import itertools
import unittest
from test import combine
import numpy as np
from ddt import ddt
from qiskit.quantum_info.random import random_unitary
from qiskit import BasicAer
from qiskit import QuantumCircuit
Expand All @@ -32,33 +33,33 @@
np.array([[0.0, 1.0], [1.0, 0.0]]),
1 / np.sqrt(2) * np.array([[1.0, 1.0], [-1.0, 1.0]]),
np.array([[np.exp(1j * 5.0 / 2), 0], [0, np.exp(-1j * 5.0 / 2)]]),
random_unitary(2).data,
random_unitary(2, seed=42).data,
]

up_to_diagonal_list = [True, False]


@ddt
class TestSingleQubitUnitary(QiskitTestCase):
"""Qiskit ZYZ-decomposition tests."""

def test_squ(self):
@combine(u=squs, up_to_diagonal=up_to_diagonal_list)
def test_squ(self, u, up_to_diagonal):
"""Tests for single-qubit unitary decomposition."""
for u, up_to_diagonal in itertools.product(squs, up_to_diagonal_list):
with self.subTest(u=u, up_to_diagonal=up_to_diagonal):
qr = QuantumRegister(1, "qr")
qc = QuantumCircuit(qr)
qc.squ(u, qr[0], up_to_diagonal=up_to_diagonal)
# Decompose the gate
qc = transpile(qc, basis_gates=["u1", "u3", "u2", "cx", "id"])
# Simulate the decomposed gate
simulator = BasicAer.get_backend("unitary_simulator")
result = execute(qc, simulator).result()
unitary = result.get_unitary(qc)
if up_to_diagonal:
squ = SingleQubitUnitary(u, up_to_diagonal=up_to_diagonal)
unitary = np.dot(np.diagflat(squ.diag), unitary)
unitary_desired = u
self.assertTrue(matrix_equal(unitary_desired, unitary, ignore_phase=True))
qr = QuantumRegister(1, "qr")
qc = QuantumCircuit(qr)
qc.squ(u, qr[0], up_to_diagonal=up_to_diagonal)
# Decompose the gate
qc = transpile(qc, basis_gates=["u1", "u3", "u2", "cx", "id"])
# Simulate the decomposed gate
simulator = BasicAer.get_backend("unitary_simulator")
result = execute(qc, simulator).result()
unitary = result.get_unitary(qc)
if up_to_diagonal:
squ = SingleQubitUnitary(u, up_to_diagonal=up_to_diagonal)
unitary = np.dot(np.diagflat(squ.diag), unitary)
unitary_desired = u
self.assertTrue(matrix_equal(unitary_desired, unitary, ignore_phase=True))


if __name__ == "__main__":
Expand Down