Skip to content

Commit

Permalink
Stabilising test_squ (#9083) (#9111)
Browse files Browse the repository at this point in the history
* stabilized squ tests

* seeding rng

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
(cherry picked from commit b224a2e)

Co-authored-by: Luciano Bello <[email protected]>
  • Loading branch information
mergify[bot] and 1ucian0 authored Nov 15, 2022
1 parent b9f3b52 commit 6948146
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
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

0 comments on commit 6948146

Please sign in to comment.