Skip to content

Commit

Permalink
Fix inverse of SdgGate in Solovay Kitaev (#9635)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Cryoris and mergify[bot] authored Feb 22, 2023
1 parent 9128ebd commit e52207f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
3 changes: 2 additions & 1 deletion qiskit/synthesis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
:toctree: ../stubs/
SolovayKitaevDecomposition
generate_basic_approximations
"""

Expand Down Expand Up @@ -107,4 +108,4 @@
synth_cnotdihedral_two_qubits,
synth_cnotdihedral_general,
)
from .discrete_basis import SolovayKitaevDecomposition
from .discrete_basis import SolovayKitaevDecomposition, generate_basic_approximations
1 change: 1 addition & 0 deletions qiskit/synthesis/discrete_basis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@
"""Discrete basis synthesis algorithms."""

from .solovay_kitaev import SolovayKitaevDecomposition
from .generate_basis_approximations import generate_basic_approximations
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"t": "tdg",
"tdg": "t",
"s": "sdg",
"sgd": "s",
"sdg": "s",
}

_1q_gates = {
Expand Down
16 changes: 14 additions & 2 deletions qiskit/transpiler/passes/synthesis/solovay_kitaev_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions releasenotes/notes/fix-sk-sdg-81ec87abe7af4a89.yaml
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/Qiskit/qiskit-terra/issues/9585>`__.
12 changes: 12 additions & 0 deletions test/python/transpiler/test_solovay_kitaev.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit e52207f

Please sign in to comment.