Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 38 additions & 17 deletions qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5305,7 +5305,8 @@ def mcrz(
self.compose(cgate, control_qubits + [target_qubit], inplace=True)

def r(
self, theta: ParameterValueType, phi: ParameterValueType, qubit: QubitSpecifier
self, theta: ParameterValueType, phi: ParameterValueType, qubit: QubitSpecifier,
label: str | None = None
) -> InstructionSet:
"""Apply :class:`~qiskit.circuit.library.RGate`.

Expand All @@ -5315,11 +5316,12 @@ def r(
theta: The angle of the rotation.
phi: The angle of the axis of rotation in the x-y plane.
qubit: The qubit(s) to apply the gate to.
label: The string label of the gate in the circuit.

Returns:
A handle to the instructions created.
"""
return self._append_standard_gate(StandardGate.R, [qubit], [theta, phi])
return self._append_standard_gate(StandardGate.R, [qubit], [theta, phi], label = label)

def rv(
self,
Expand Down Expand Up @@ -5353,6 +5355,7 @@ def rccx(
control_qubit1: QubitSpecifier,
control_qubit2: QubitSpecifier,
target_qubit: QubitSpecifier,
label: str | None = None
) -> InstructionSet:
"""Apply :class:`~qiskit.circuit.library.RCCXGate`.

Expand All @@ -5362,12 +5365,13 @@ def rccx(
control_qubit1: The qubit(s) used as the first control.
control_qubit2: The qubit(s) used as the second control.
target_qubit: The qubit(s) targeted by the gate.
label: The string label of the gate in the circuit.

Returns:
A handle to the instructions created.
"""
return self._append_standard_gate(
StandardGate.RCCX, [control_qubit1, control_qubit2, target_qubit], ()
StandardGate.RCCX, [control_qubit1, control_qubit2, target_qubit], (), label = label
)

def rcccx(
Expand All @@ -5376,6 +5380,7 @@ def rcccx(
control_qubit2: QubitSpecifier,
control_qubit3: QubitSpecifier,
target_qubit: QubitSpecifier,
label: str | None = None
) -> InstructionSet:
"""Apply :class:`~qiskit.circuit.library.RC3XGate`.

Expand All @@ -5386,6 +5391,7 @@ def rcccx(
control_qubit2: The qubit(s) used as the second control.
control_qubit3: The qubit(s) used as the third control.
target_qubit: The qubit(s) targeted by the gate.
label: The string label of the gate in the circuit.

Returns:
A handle to the instructions created.
Expand All @@ -5394,6 +5400,7 @@ def rcccx(
StandardGate.RC3X,
[control_qubit1, control_qubit2, control_qubit3, target_qubit],
(),
label = label
)

def rx(
Expand Down Expand Up @@ -5453,7 +5460,8 @@ def crx(
)

def rxx(
self, theta: ParameterValueType, qubit1: QubitSpecifier, qubit2: QubitSpecifier
self, theta: ParameterValueType, qubit1: QubitSpecifier, qubit2: QubitSpecifier,
label: str | None = None
) -> InstructionSet:
"""Apply :class:`~qiskit.circuit.library.RXXGate`.

Expand All @@ -5463,11 +5471,12 @@ def rxx(
theta: The angle of the rotation.
qubit1: The qubit(s) to apply the gate to.
qubit2: The qubit(s) to apply the gate to.
label: The string label of the gate in the circuit.

Returns:
A handle to the instructions created.
"""
return self._append_standard_gate(StandardGate.RXX, [qubit1, qubit2], [theta])
return self._append_standard_gate(StandardGate.RXX, [qubit1, qubit2], [theta], label = label)

def ry(
self, theta: ParameterValueType, qubit: QubitSpecifier, label: str | None = None
Expand Down Expand Up @@ -5526,7 +5535,8 @@ def cry(
)

def ryy(
self, theta: ParameterValueType, qubit1: QubitSpecifier, qubit2: QubitSpecifier
self, theta: ParameterValueType, qubit1: QubitSpecifier, qubit2: QubitSpecifier,
label: str | None = None
) -> InstructionSet:
"""Apply :class:`~qiskit.circuit.library.RYYGate`.

Expand All @@ -5536,25 +5546,28 @@ def ryy(
theta: The rotation angle of the gate.
qubit1: The qubit(s) to apply the gate to.
qubit2: The qubit(s) to apply the gate to.
label: The string label of the gate in the circuit.

Returns:
A handle to the instructions created.
"""
return self._append_standard_gate(StandardGate.RYY, [qubit1, qubit2], [theta])
return self._append_standard_gate(StandardGate.RYY, [qubit1, qubit2], [theta], label = label)

def rz(self, phi: ParameterValueType, qubit: QubitSpecifier) -> InstructionSet:
def rz(self, phi: ParameterValueType, qubit: QubitSpecifier,
label: str | None = None) -> InstructionSet:
"""Apply :class:`~qiskit.circuit.library.RZGate`.

For the full matrix form of this gate, see the underlying gate documentation.

Args:
phi: The rotation angle of the gate.
qubit: The qubit(s) to apply the gate to.
label: The string label of the gate in the circuit.

Returns:
A handle to the instructions created.
"""
return self._append_standard_gate(StandardGate.RZ, [qubit], [phi])
return self._append_standard_gate(StandardGate.RZ, [qubit], [phi], label = label)

def crz(
self,
Expand Down Expand Up @@ -5596,7 +5609,8 @@ def crz(
)

def rzx(
self, theta: ParameterValueType, qubit1: QubitSpecifier, qubit2: QubitSpecifier
self, theta: ParameterValueType, qubit1: QubitSpecifier, qubit2: QubitSpecifier,
label: str | None = None
) -> InstructionSet:
"""Apply :class:`~qiskit.circuit.library.RZXGate`.

Expand All @@ -5606,14 +5620,17 @@ def rzx(
theta: The rotation angle of the gate.
qubit1: The qubit(s) to apply the gate to.
qubit2: The qubit(s) to apply the gate to.
label: The string label of the gate in the circuit.


Returns:
A handle to the instructions created.
"""
return self._append_standard_gate(StandardGate.RZX, [qubit1, qubit2], [theta])
return self._append_standard_gate(StandardGate.RZX, [qubit1, qubit2], [theta], label = label)

def rzz(
self, theta: ParameterValueType, qubit1: QubitSpecifier, qubit2: QubitSpecifier
self, theta: ParameterValueType, qubit1: QubitSpecifier, qubit2: QubitSpecifier,
label: str | None = None
) -> InstructionSet:
"""Apply :class:`~qiskit.circuit.library.RZZGate`.

Expand All @@ -5623,37 +5640,41 @@ def rzz(
theta: The rotation angle of the gate.
qubit1: The qubit(s) to apply the gate to.
qubit2: The qubit(s) to apply the gate to.
label: The string label of the gate in the circuit.

Returns:
A handle to the instructions created.
"""
return self._append_standard_gate(StandardGate.RZZ, [qubit1, qubit2], [theta])
return self._append_standard_gate(StandardGate.RZZ, [qubit1, qubit2], [theta], label = label)

def ecr(self, qubit1: QubitSpecifier, qubit2: QubitSpecifier) -> InstructionSet:
def ecr(self, qubit1: QubitSpecifier, qubit2: QubitSpecifier,
label: str | None = None) -> InstructionSet:
"""Apply :class:`~qiskit.circuit.library.ECRGate`.

For the full matrix form of this gate, see the underlying gate documentation.

Args:
qubit1, qubit2: The qubits to apply the gate to.
label: The string label of the gate in the circuit.

Returns:
A handle to the instructions created.
"""
return self._append_standard_gate(StandardGate.ECR, [qubit1, qubit2], ())
return self._append_standard_gate(StandardGate.ECR, [qubit1, qubit2], (), label = label)

def s(self, qubit: QubitSpecifier) -> InstructionSet:
def s(self, qubit: QubitSpecifier, label: str | None = None) -> InstructionSet:
"""Apply :class:`~qiskit.circuit.library.SGate`.

For the full matrix form of this gate, see the underlying gate documentation.

Args:
qubit: The qubit(s) to apply the gate to.
label: The string label of the gate in the circuit.

Returns:
A handle to the instructions created.
"""
return self._append_standard_gate(StandardGate.S, [qubit], ())
return self._append_standard_gate(StandardGate.S, [qubit], (), label = label)

def sdg(self, qubit: QubitSpecifier) -> InstructionSet:
"""Apply :class:`~qiskit.circuit.library.SdgGate`.
Expand Down
17 changes: 17 additions & 0 deletions test/python/circuit/test_gate_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from test import QiskitTestCase
import numpy as np
from qiskit import QuantumCircuit


class TestGateLabels(QiskitTestCase):
def test_gate_labels_are_applied(self):
Comment on lines +6 to +7
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we put this test function in an existing class in test/python/circuit/test_circuit_operations.py, just to avoid an extra stub file? There's no perfect place for it, but I think we can avoid the new file.

qc = QuantumCircuit(2)
# Make gates with labels
qc.rz(np.pi, 0, label="rz_gate")
qc.rxx(np.pi, 0, 1, "rxx_gate")
qc.s(0, "s_gate")

# Check labels are there
assert qc.data[0].label == "rz_gate"
assert qc.data[1].label == "rxx_gate"
assert qc.data[2].label == "s_gate"
Comment on lines +14 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Qiskit uses the built-in unittest runner, so these should be self.assertEqual(qc.data[0].label, "rz_gate") to handle assertion failures / messages better.