From 3d676d2fd0d0ce0989bc4bcc4c8937d630b340ba Mon Sep 17 00:00:00 2001 From: Andy Maloney Date: Wed, 17 Apr 2024 15:14:51 -0500 Subject: [PATCH] Update gate dictionary in `two_local.py` (#12009) * Update gate dictionary in `two_local.py` This commit removes the hard coded gate dictionary with the one generated by the method `get_standard_gate_name_mapping`. Resolves #1202 * Release note * fix typo * Tweak release-note wording * Add explicit test --------- Co-authored-by: Jake Lishman --- qiskit/circuit/library/n_local/two_local.py | 62 +------------------ ...date-gate-dictionary-c0c017be67bb2f29.yaml | 7 +++ test/python/circuit/library/test_nlocal.py | 11 ++++ 3 files changed, 21 insertions(+), 59 deletions(-) create mode 100644 releasenotes/notes/update-gate-dictionary-c0c017be67bb2f29.yaml diff --git a/qiskit/circuit/library/n_local/two_local.py b/qiskit/circuit/library/n_local/two_local.py index 06637730014..61b9e725cc6 100644 --- a/qiskit/circuit/library/n_local/two_local.py +++ b/qiskit/circuit/library/n_local/two_local.py @@ -17,35 +17,10 @@ from collections.abc import Callable, Sequence from qiskit.circuit.quantumcircuit import QuantumCircuit -from qiskit.circuit import Gate, Instruction, Parameter +from qiskit.circuit import Gate, Instruction from .n_local import NLocal -from ..standard_gates import ( - IGate, - XGate, - YGate, - ZGate, - RXGate, - RYGate, - RZGate, - HGate, - SGate, - SdgGate, - TGate, - TdgGate, - RXXGate, - RYYGate, - RZXGate, - RZZGate, - SwapGate, - CXGate, - CYGate, - CZGate, - CRXGate, - CRYGate, - CRZGate, - CHGate, -) +from ..standard_gates import get_standard_gate_name_mapping if typing.TYPE_CHECKING: import qiskit # pylint: disable=cyclic-import @@ -269,38 +244,7 @@ def _convert_to_block(self, layer: str | type | Gate | QuantumCircuit) -> Quantu if isinstance(layer, QuantumCircuit): return layer - # check the list of valid layers - # this could be a lot easier if the standard layers would have ``name`` and ``num_params`` - # as static types, which might be something they should have anyway - theta = Parameter("θ") - valid_layers = { - "ch": CHGate(), - "cx": CXGate(), - "cy": CYGate(), - "cz": CZGate(), - "crx": CRXGate(theta), - "cry": CRYGate(theta), - "crz": CRZGate(theta), - "h": HGate(), - "i": IGate(), - "id": IGate(), - "iden": IGate(), - "rx": RXGate(theta), - "rxx": RXXGate(theta), - "ry": RYGate(theta), - "ryy": RYYGate(theta), - "rz": RZGate(theta), - "rzx": RZXGate(theta), - "rzz": RZZGate(theta), - "s": SGate(), - "sdg": SdgGate(), - "swap": SwapGate(), - "x": XGate(), - "y": YGate(), - "z": ZGate(), - "t": TGate(), - "tdg": TdgGate(), - } + valid_layers = get_standard_gate_name_mapping() # try to exchange `layer` from a string to a gate instance if isinstance(layer, str): diff --git a/releasenotes/notes/update-gate-dictionary-c0c017be67bb2f29.yaml b/releasenotes/notes/update-gate-dictionary-c0c017be67bb2f29.yaml new file mode 100644 index 00000000000..b73d2088671 --- /dev/null +++ b/releasenotes/notes/update-gate-dictionary-c0c017be67bb2f29.yaml @@ -0,0 +1,7 @@ +--- +features_circuits: + - | + All of the "standard gates" in the circuit library + (:mod:`qiskit.circuit.library`) can now be specified by string name for + the entangling operations in :class:`.TwoLocal` circuits, such as + :class:`.RealAmplitudes` and :class:`.EfficientSU2`. diff --git a/test/python/circuit/library/test_nlocal.py b/test/python/circuit/library/test_nlocal.py index fc003328b1a..2e308af48ff 100644 --- a/test/python/circuit/library/test_nlocal.py +++ b/test/python/circuit/library/test_nlocal.py @@ -37,6 +37,7 @@ RXXGate, RYYGate, CXGate, + SXGate, ) from qiskit.circuit.random.utils import random_circuit from qiskit.converters.circuit_to_dag import circuit_to_dag @@ -697,6 +698,16 @@ def test_ryrz_blocks(self): expected = [(-np.pi, np.pi)] * two.num_parameters np.testing.assert_almost_equal(two.parameter_bounds, expected) + def test_rzsx_blocks(self): + """Test that the EfficientSU2 circuit is instantiated correctly.""" + two = EfficientSU2(3, ["rz", "sx", "rz", "sx", "rz"]) + expected = [[RZGate], [SXGate], [RZGate], [SXGate], [RZGate]] + actual = [ + [instruction.operation.base_class for instruction in block] + for block in two.rotation_blocks + ] + self.assertEqual(actual, expected) + def test_ryrz_circuit(self): """Test an EfficientSU2 circuit.""" num_qubits = 3