From e52207f583dd3a4134e52d19fc41781fef5e4123 Mon Sep 17 00:00:00 2001 From: Julien Gacon Date: Wed, 22 Feb 2023 19:32:36 +0100 Subject: [PATCH] Fix inverse of ``SdgGate`` in Solovay Kitaev (#9635) * Fix typo and add regression test * add example and include basic_approx in API doc * use assertTrue * use `assertGreater` * run black with the right version this time --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- qiskit/synthesis/__init__.py | 3 ++- qiskit/synthesis/discrete_basis/__init__.py | 1 + .../generate_basis_approximations.py | 2 +- .../passes/synthesis/solovay_kitaev_synthesis.py | 16 ++++++++++++++-- .../notes/fix-sk-sdg-81ec87abe7af4a89.yaml | 6 ++++++ test/python/transpiler/test_solovay_kitaev.py | 12 ++++++++++++ 6 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 releasenotes/notes/fix-sk-sdg-81ec87abe7af4a89.yaml diff --git a/qiskit/synthesis/__init__.py b/qiskit/synthesis/__init__.py index 1618e7346b50..7fed339a59e2 100644 --- a/qiskit/synthesis/__init__.py +++ b/qiskit/synthesis/__init__.py @@ -77,6 +77,7 @@ :toctree: ../stubs/ SolovayKitaevDecomposition + generate_basic_approximations """ @@ -107,4 +108,4 @@ synth_cnotdihedral_two_qubits, synth_cnotdihedral_general, ) -from .discrete_basis import SolovayKitaevDecomposition +from .discrete_basis import SolovayKitaevDecomposition, generate_basic_approximations diff --git a/qiskit/synthesis/discrete_basis/__init__.py b/qiskit/synthesis/discrete_basis/__init__.py index 76ab9821c64c..3b7d20a2be2c 100644 --- a/qiskit/synthesis/discrete_basis/__init__.py +++ b/qiskit/synthesis/discrete_basis/__init__.py @@ -13,3 +13,4 @@ """Discrete basis synthesis algorithms.""" from .solovay_kitaev import SolovayKitaevDecomposition +from .generate_basis_approximations import generate_basic_approximations diff --git a/qiskit/synthesis/discrete_basis/generate_basis_approximations.py b/qiskit/synthesis/discrete_basis/generate_basis_approximations.py index 5fe8302b1ea4..2839137a2c59 100644 --- a/qiskit/synthesis/discrete_basis/generate_basis_approximations.py +++ b/qiskit/synthesis/discrete_basis/generate_basis_approximations.py @@ -36,7 +36,7 @@ "t": "tdg", "tdg": "t", "s": "sdg", - "sgd": "s", + "sdg": "s", } _1q_gates = { diff --git a/qiskit/transpiler/passes/synthesis/solovay_kitaev_synthesis.py b/qiskit/transpiler/passes/synthesis/solovay_kitaev_synthesis.py index 43ae079b9fc2..c7440719e4ad 100644 --- a/qiskit/transpiler/passes/synthesis/solovay_kitaev_synthesis.py +++ b/qiskit/transpiler/passes/synthesis/solovay_kitaev_synthesis.py @@ -63,11 +63,12 @@ class SolovayKitaev(TransformationPass): Examples: + Per default, the basis gate set is ``["t", "tdg", "h"]``: + .. code-block:: import numpy as np from qiskit.circuit import QuantumCircuit - from qiskit.circuit.library import TGate, HGate, TdgGate from qiskit.transpiler.passes.synthesis import SolovayKitaev from qiskit.quantum_info import Operator @@ -77,7 +78,6 @@ class SolovayKitaev(TransformationPass): print("Original circuit:") print(circuit.draw()) - basis_gates = [TGate(), TdgGate(), HGate()] skd = SolovayKitaev(recursion_degree=2) discretized = skd(circuit) @@ -100,6 +100,18 @@ class SolovayKitaev(TransformationPass): └───┘└───┘└───┘ Error: 2.828408279166474 + For individual basis gate sets, the ``generate_basic_approximations`` function can be used: + + .. code-block:: + + from qiskit.synthesis import generate_basic_approximations + from qiskit.transpiler.passes import SolovayKitaev + + basis = ["s", "sdg", "t", "tdg", "z", "h"] + approx = generate_basic_approximations(basis, depth=3) + + skd = SolovayKitaev(recursion_degree=2, basic_approximations=approx) + References: [1]: Kitaev, A Yu (1997). Quantum computations: algorithms and error correction. diff --git a/releasenotes/notes/fix-sk-sdg-81ec87abe7af4a89.yaml b/releasenotes/notes/fix-sk-sdg-81ec87abe7af4a89.yaml new file mode 100644 index 000000000000..bd20da12e4fc --- /dev/null +++ b/releasenotes/notes/fix-sk-sdg-81ec87abe7af4a89.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Fixed a bug in :func:`.generate_basic_approximations` where the inverse of the + :class:`.SdgGate` was not correctly recognized as :class:`.SGate`. + Fixed `#9585 `__. diff --git a/test/python/transpiler/test_solovay_kitaev.py b/test/python/transpiler/test_solovay_kitaev.py index d3d85b8cfb76..d089334b29d5 100644 --- a/test/python/transpiler/test_solovay_kitaev.py +++ b/test/python/transpiler/test_solovay_kitaev.py @@ -394,6 +394,18 @@ def test_commutator_decompose_decomposes_correctly(self, u_so3): actual_commutator = np.dot(v_so3, np.dot(w_so3, np.dot(np.conj(v_so3).T, np.conj(w_so3).T))) self.assertTrue(np.allclose(actual_commutator, u_so3)) + def test_generate_basis_approximation_gates(self): + """Test the basis approximation generation works for all supported gates. + + Regression test of Qiskit/qiskit-terra#9585. + """ + basis = ["i", "x", "y", "z", "h", "t", "tdg", "s", "sdg"] + approx = generate_basic_approximations(basis, depth=2) + + # This mainly checks that there are no errors in the generation (like + # in computing the inverse as described in #9585), so a simple check is enough. + self.assertGreater(len(approx), len(basis)) + if __name__ == "__main__": unittest.main()