Skip to content

Commit

Permalink
Update gate dictionary in two_local.py (#12009)
Browse files Browse the repository at this point in the history
* 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 <jake.lishman@ibm.com>
  • Loading branch information
amaloney and jakelishman committed Apr 17, 2024
1 parent 5f53524 commit 3d676d2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 59 deletions.
62 changes: 3 additions & 59 deletions qiskit/circuit/library/n_local/two_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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`.
11 changes: 11 additions & 0 deletions test/python/circuit/library/test_nlocal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3d676d2

Please sign in to comment.