From 0783fbb6e898593c876b95639f1e9ad1b83c8e8c Mon Sep 17 00:00:00 2001 From: AlexanderIvrii Date: Wed, 31 Jan 2024 16:29:56 +0200 Subject: [PATCH] simplifying logic based on code review --- .../circuit/library/generalized_gates/mcmt.py | 14 +--- qiskit/circuit/library/standard_gates/h.py | 20 ++--- qiskit/circuit/library/standard_gates/p.py | 78 ++++++++----------- qiskit/circuit/library/standard_gates/rx.py | 22 ++---- qiskit/circuit/library/standard_gates/ry.py | 22 ++---- qiskit/circuit/library/standard_gates/rz.py | 22 ++---- qiskit/circuit/library/standard_gates/swap.py | 20 ++--- qiskit/circuit/library/standard_gates/sx.py | 20 ++--- qiskit/circuit/library/standard_gates/u.py | 36 ++++----- qiskit/circuit/library/standard_gates/u1.py | 56 ++++++------- qiskit/circuit/library/standard_gates/u3.py | 22 ++---- qiskit/circuit/library/standard_gates/x.py | 59 ++++++++------ qiskit/circuit/library/standard_gates/y.py | 20 ++--- qiskit/circuit/library/standard_gates/z.py | 20 ++--- 14 files changed, 174 insertions(+), 257 deletions(-) diff --git a/qiskit/circuit/library/generalized_gates/mcmt.py b/qiskit/circuit/library/generalized_gates/mcmt.py index e36b87dd4c03..c584d20317e5 100644 --- a/qiskit/circuit/library/generalized_gates/mcmt.py +++ b/qiskit/circuit/library/generalized_gates/mcmt.py @@ -18,7 +18,6 @@ from qiskit import circuit from qiskit.circuit import ControlledGate, Gate, QuantumRegister, QuantumCircuit -from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier from qiskit.exceptions import QiskitError # pylint: disable=cyclic-import @@ -145,17 +144,10 @@ def _identify_gate(self, gate): def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None, annotated=False): """Return the controlled version of the MCMT circuit.""" - if not annotated: - if ctrl_state is None: # TODO add ctrl state implementation by adding X gates - gate = MCMT( - self.gate, self.num_ctrl_qubits + num_ctrl_qubits, self.num_target_qubits - ) - else: - gate = super().control(num_ctrl_qubits, label, ctrl_state, annotated=annotated) + if not annotated and ctrl_state is None: + gate = MCMT(self.gate, self.num_ctrl_qubits + num_ctrl_qubits, self.num_target_qubits) else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) - ) + gate = super().control(num_ctrl_qubits, label, ctrl_state, annotated=annotated) return gate def inverse(self): diff --git a/qiskit/circuit/library/standard_gates/h.py b/qiskit/circuit/library/standard_gates/h.py index afaa3a935a60..9184241e7537 100644 --- a/qiskit/circuit/library/standard_gates/h.py +++ b/qiskit/circuit/library/standard_gates/h.py @@ -15,7 +15,6 @@ from typing import Optional, Union import numpy from qiskit.circuit.singleton import SingletonGate, SingletonControlledGate, stdlib_singleton_key -from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier from qiskit.circuit.quantumregister import QuantumRegister from qiskit.circuit._utils import with_gate_array, with_controlled_gate_array @@ -96,19 +95,14 @@ def control( Returns: ControlledGate: controlled version of this gate. """ - if not annotated: - if num_ctrl_qubits == 1: - gate = CHGate(label=label, ctrl_state=ctrl_state, _base_label=self.label) - else: - gate = super().control( - num_ctrl_qubits=num_ctrl_qubits, - label=label, - ctrl_state=ctrl_state, - annotated=annotated, - ) + if not annotated and num_ctrl_qubits == 1: + gate = CHGate(label=label, ctrl_state=ctrl_state, _base_label=self.label) else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate diff --git a/qiskit/circuit/library/standard_gates/p.py b/qiskit/circuit/library/standard_gates/p.py index 2ab8700cc49b..23bb87f81628 100644 --- a/qiskit/circuit/library/standard_gates/p.py +++ b/qiskit/circuit/library/standard_gates/p.py @@ -16,7 +16,6 @@ from cmath import exp import numpy from qiskit.circuit.controlledgate import ControlledGate -from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier from qiskit.circuit.gate import Gate from qiskit.circuit.quantumregister import QuantumRegister from qiskit.circuit.parameterexpression import ParameterValueType @@ -112,23 +111,18 @@ def control( Returns: ControlledGate: controlled version of this gate. """ - if not annotated: - if num_ctrl_qubits == 1: - gate = CPhaseGate(self.params[0], label=label, ctrl_state=ctrl_state) - gate.base_gate.label = self.label - elif ctrl_state is None and num_ctrl_qubits > 1: - gate = MCPhaseGate(self.params[0], num_ctrl_qubits, label=label) - gate.base_gate.label = self.label - else: - gate = super().control( - num_ctrl_qubits=num_ctrl_qubits, - label=label, - ctrl_state=ctrl_state, - annotated=annotated, - ) + if not annotated and num_ctrl_qubits == 1: + gate = CPhaseGate(self.params[0], label=label, ctrl_state=ctrl_state) + gate.base_gate.label = self.label + elif not annotated and ctrl_state is None and num_ctrl_qubits > 1: + gate = MCPhaseGate(self.params[0], num_ctrl_qubits, label=label) + gate.base_gate.label = self.label else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate @@ -255,20 +249,15 @@ def control( Returns: ControlledGate: controlled version of this gate. """ - if not annotated: - if ctrl_state is None: - gate = MCPhaseGate(self.params[0], num_ctrl_qubits=num_ctrl_qubits + 1, label=label) - gate.base_gate.label = self.label - else: - gate = super().control( - num_ctrl_qubits=num_ctrl_qubits, - label=label, - ctrl_state=ctrl_state, - annotated=annotated, - ) + if not annotated and ctrl_state is None: + gate = MCPhaseGate(self.params[0], num_ctrl_qubits=num_ctrl_qubits + 1, label=label) + gate.base_gate.label = self.label else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate @@ -381,24 +370,19 @@ def control( Returns: ControlledGate: controlled version of this gate. """ - if not annotated: - if ctrl_state is None: - gate = MCPhaseGate( - self.params[0], - num_ctrl_qubits=num_ctrl_qubits + self.num_ctrl_qubits, - label=label, - ) - gate.base_gate.label = self.label - else: - gate = super().control( - num_ctrl_qubits=num_ctrl_qubits, - label=label, - ctrl_state=ctrl_state, - annotated=annotated, - ) + if not annotated and ctrl_state is None: + gate = MCPhaseGate( + self.params[0], + num_ctrl_qubits=num_ctrl_qubits + self.num_ctrl_qubits, + label=label, + ) + gate.base_gate.label = self.label else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate diff --git a/qiskit/circuit/library/standard_gates/rx.py b/qiskit/circuit/library/standard_gates/rx.py index 76e978a81422..e8fdd586ca60 100644 --- a/qiskit/circuit/library/standard_gates/rx.py +++ b/qiskit/circuit/library/standard_gates/rx.py @@ -18,7 +18,6 @@ import numpy from qiskit.circuit.controlledgate import ControlledGate -from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier from qiskit.circuit.gate import Gate from qiskit.circuit.quantumregister import QuantumRegister from qiskit.circuit.parameterexpression import ParameterValueType @@ -93,20 +92,15 @@ def control( Returns: ControlledGate: controlled version of this gate. """ - if not annotated: - if num_ctrl_qubits == 1: - gate = CRXGate(self.params[0], label=label, ctrl_state=ctrl_state) - gate.base_gate.label = self.label - else: - gate = super().control( - num_ctrl_qubits=num_ctrl_qubits, - label=label, - ctrl_state=ctrl_state, - annotated=annotated, - ) + if not annotated and num_ctrl_qubits == 1: + gate = CRXGate(self.params[0], label=label, ctrl_state=ctrl_state) + gate.base_gate.label = self.label else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate diff --git a/qiskit/circuit/library/standard_gates/ry.py b/qiskit/circuit/library/standard_gates/ry.py index 0784806c1ff8..fedef72345fc 100644 --- a/qiskit/circuit/library/standard_gates/ry.py +++ b/qiskit/circuit/library/standard_gates/ry.py @@ -17,7 +17,6 @@ from typing import Optional, Union import numpy from qiskit.circuit.controlledgate import ControlledGate -from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier from qiskit.circuit.gate import Gate from qiskit.circuit.quantumregister import QuantumRegister from qiskit.circuit.parameterexpression import ParameterValueType @@ -92,20 +91,15 @@ def control( Returns: ControlledGate: controlled version of this gate. """ - if not annotated: - if num_ctrl_qubits == 1: - gate = CRYGate(self.params[0], label=label, ctrl_state=ctrl_state) - gate.base_gate.label = self.label - else: - gate = super().control( - num_ctrl_qubits=num_ctrl_qubits, - label=label, - ctrl_state=ctrl_state, - annotated=annotated, - ) + if not annotated and num_ctrl_qubits == 1: + gate = CRYGate(self.params[0], label=label, ctrl_state=ctrl_state) + gate.base_gate.label = self.label else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate diff --git a/qiskit/circuit/library/standard_gates/rz.py b/qiskit/circuit/library/standard_gates/rz.py index b8181539c5ba..185c8d76a544 100644 --- a/qiskit/circuit/library/standard_gates/rz.py +++ b/qiskit/circuit/library/standard_gates/rz.py @@ -15,7 +15,6 @@ from typing import Optional, Union from qiskit.circuit.gate import Gate from qiskit.circuit.controlledgate import ControlledGate -from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier from qiskit.circuit.quantumregister import QuantumRegister from qiskit.circuit.parameterexpression import ParameterValueType @@ -103,20 +102,15 @@ def control( Returns: ControlledGate: controlled version of this gate. """ - if not annotated: - if num_ctrl_qubits == 1: - gate = CRZGate(self.params[0], label=label, ctrl_state=ctrl_state) - gate.base_gate.label = self.label - else: - gate = super().control( - num_ctrl_qubits=num_ctrl_qubits, - label=label, - ctrl_state=ctrl_state, - annotated=annotated, - ) + if not annotated and num_ctrl_qubits == 1: + gate = CRZGate(self.params[0], label=label, ctrl_state=ctrl_state) + gate.base_gate.label = self.label else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate diff --git a/qiskit/circuit/library/standard_gates/swap.py b/qiskit/circuit/library/standard_gates/swap.py index 143aa1381c83..1dc8b5936026 100644 --- a/qiskit/circuit/library/standard_gates/swap.py +++ b/qiskit/circuit/library/standard_gates/swap.py @@ -15,7 +15,6 @@ from typing import Optional, Union import numpy from qiskit.circuit.singleton import SingletonGate, SingletonControlledGate, stdlib_singleton_key -from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier from qiskit.circuit.quantumregister import QuantumRegister from qiskit.circuit._utils import with_gate_array, with_controlled_gate_array @@ -107,19 +106,14 @@ def control( Returns: ControlledGate: controlled version of this gate. """ - if not annotated: - if num_ctrl_qubits == 1: - gate = CSwapGate(label=label, ctrl_state=ctrl_state, _base_label=self.label) - else: - gate = super().control( - num_ctrl_qubits=num_ctrl_qubits, - label=label, - ctrl_state=ctrl_state, - annotated=annotated, - ) + if not annotated and num_ctrl_qubits == 1: + gate = CSwapGate(label=label, ctrl_state=ctrl_state, _base_label=self.label) else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate diff --git a/qiskit/circuit/library/standard_gates/sx.py b/qiskit/circuit/library/standard_gates/sx.py index ad48562410ac..0b39933c3a01 100644 --- a/qiskit/circuit/library/standard_gates/sx.py +++ b/qiskit/circuit/library/standard_gates/sx.py @@ -15,7 +15,6 @@ from math import pi from typing import Optional, Union from qiskit.circuit.singleton import SingletonGate, SingletonControlledGate, stdlib_singleton_key -from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier from qiskit.circuit.quantumregister import QuantumRegister from qiskit.circuit._utils import with_gate_array, with_controlled_gate_array @@ -111,19 +110,14 @@ def control( Returns: SingletonControlledGate: controlled version of this gate. """ - if not annotated: - if num_ctrl_qubits == 1: - gate = CSXGate(label=label, ctrl_state=ctrl_state, _base_label=self.label) - else: - gate = super().control( - num_ctrl_qubits=num_ctrl_qubits, - label=label, - ctrl_state=ctrl_state, - annotated=annotated, - ) + if not annotated and num_ctrl_qubits == 1: + gate = CSXGate(label=label, ctrl_state=ctrl_state, _base_label=self.label) else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate diff --git a/qiskit/circuit/library/standard_gates/u.py b/qiskit/circuit/library/standard_gates/u.py index 2db9e7df1faa..13a017bcf9d6 100644 --- a/qiskit/circuit/library/standard_gates/u.py +++ b/qiskit/circuit/library/standard_gates/u.py @@ -17,7 +17,6 @@ from typing import Optional, Union import numpy from qiskit.circuit.controlledgate import ControlledGate -from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier from qiskit.circuit.gate import Gate from qiskit.circuit.parameterexpression import ParameterValueType from qiskit.circuit.quantumregister import QuantumRegister @@ -108,27 +107,22 @@ def control( Returns: ControlledGate: controlled version of this gate. """ - if not annotated: - if num_ctrl_qubits == 1: - gate = CUGate( - self.params[0], - self.params[1], - self.params[2], - 0, - label=label, - ctrl_state=ctrl_state, - ) - gate.base_gate.label = self.label - else: - gate = super().control( - num_ctrl_qubits=num_ctrl_qubits, - label=label, - ctrl_state=ctrl_state, - annotated=annotated, - ) + if not annotated and num_ctrl_qubits == 1: + gate = CUGate( + self.params[0], + self.params[1], + self.params[2], + 0, + label=label, + ctrl_state=ctrl_state, + ) + gate.base_gate.label = self.label else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate diff --git a/qiskit/circuit/library/standard_gates/u1.py b/qiskit/circuit/library/standard_gates/u1.py index 33c05bf76b69..08df09c1900d 100644 --- a/qiskit/circuit/library/standard_gates/u1.py +++ b/qiskit/circuit/library/standard_gates/u1.py @@ -15,7 +15,6 @@ from cmath import exp import numpy from qiskit.circuit.controlledgate import ControlledGate -from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier from qiskit.circuit.gate import Gate from qiskit.circuit.parameterexpression import ParameterValueType from qiskit.circuit.quantumregister import QuantumRegister @@ -132,23 +131,18 @@ def control( Returns: ControlledGate: controlled version of this gate. """ - if not annotated: - if num_ctrl_qubits == 1: - gate = CU1Gate(self.params[0], label=label, ctrl_state=ctrl_state) - gate.base_gate.label = self.label - elif ctrl_state is None and num_ctrl_qubits > 1: - gate = MCU1Gate(self.params[0], num_ctrl_qubits, label=label) - gate.base_gate.label = self.label - else: - gate = super().control( - num_ctrl_qubits=num_ctrl_qubits, - label=label, - ctrl_state=ctrl_state, - annotated=annotated, - ) + if not annotated and num_ctrl_qubits == 1: + gate = CU1Gate(self.params[0], label=label, ctrl_state=ctrl_state) + gate.base_gate.label = self.label + elif not annotated and ctrl_state is None and num_ctrl_qubits > 1: + gate = MCU1Gate(self.params[0], num_ctrl_qubits, label=label) + gate.base_gate.label = self.label else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate @@ -273,20 +267,15 @@ def control( Returns: ControlledGate: controlled version of this gate. """ - if not annotated: - if ctrl_state is None: - gate = MCU1Gate(self.params[0], num_ctrl_qubits=num_ctrl_qubits + 1, label=label) - gate.base_gate.label = self.label - else: - gate = super().control( - num_ctrl_qubits=num_ctrl_qubits, - label=label, - ctrl_state=ctrl_state, - annotated=annotated, - ) + if not annotated and ctrl_state is None: + gate = MCU1Gate(self.params[0], num_ctrl_qubits=num_ctrl_qubits + 1, label=label) + gate.base_gate.label = self.label else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate @@ -408,8 +397,11 @@ def control( ) gate.base_gate.label = self.label else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate diff --git a/qiskit/circuit/library/standard_gates/u3.py b/qiskit/circuit/library/standard_gates/u3.py index e1f7370abcf0..0394aeba7b48 100644 --- a/qiskit/circuit/library/standard_gates/u3.py +++ b/qiskit/circuit/library/standard_gates/u3.py @@ -16,7 +16,6 @@ from typing import Optional, Union import numpy from qiskit.circuit.controlledgate import ControlledGate -from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier from qiskit.circuit.gate import Gate from qiskit.circuit.parameterexpression import ParameterValueType from qiskit.circuit.quantumregister import QuantumRegister @@ -120,20 +119,15 @@ def control( Returns: ControlledGate: controlled version of this gate. """ - if not annotated: - if num_ctrl_qubits == 1: - gate = CU3Gate(*self.params, label=label, ctrl_state=ctrl_state) - gate.base_gate.label = self.label - else: - gate = super().control( - num_ctrl_qubits=num_ctrl_qubits, - label=label, - ctrl_state=ctrl_state, - annotated=annotated, - ) + if not annotated and num_ctrl_qubits == 1: + gate = CU3Gate(*self.params, label=label, ctrl_state=ctrl_state) + gate.base_gate.label = self.label else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate diff --git a/qiskit/circuit/library/standard_gates/x.py b/qiskit/circuit/library/standard_gates/x.py index bdb31c1a8926..35da5672e421 100644 --- a/qiskit/circuit/library/standard_gates/x.py +++ b/qiskit/circuit/library/standard_gates/x.py @@ -17,7 +17,6 @@ import numpy from qiskit.utils.deprecation import deprecate_func from qiskit.circuit.controlledgate import ControlledGate -from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier from qiskit.circuit.singleton import SingletonGate, SingletonControlledGate, stdlib_singleton_key from qiskit.circuit.quantumregister import QuantumRegister from qiskit.circuit._utils import _ctrl_state_to_int, with_gate_array, with_controlled_gate_array @@ -124,8 +123,11 @@ def control( _base_label=self.label, ) else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate @@ -253,8 +255,11 @@ def control( _base_label=self.label, ) else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate @@ -431,8 +436,11 @@ def control( _base_label=self.label, ) else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate @@ -768,8 +776,11 @@ def control( _base_label=self.label, ) else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate @@ -993,8 +1004,11 @@ def control( _base_label=self.label, ) else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate @@ -1121,21 +1135,16 @@ def control( Returns: ControlledGate: controlled version of this gate. """ - if not annotated: - if ctrl_state is None: - # use __class__ so this works for derived classes - gate = self.__class__( - self.num_ctrl_qubits + num_ctrl_qubits, - label=label, - ctrl_state=ctrl_state, - _base_label=self.label, - ) - else: - gate = super().control(num_ctrl_qubits, label=label, ctrl_state=ctrl_state) - else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + if not annotated and ctrl_state is None: + # use __class__ so this works for derived classes + gate = self.__class__( + self.num_ctrl_qubits + num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + _base_label=self.label, ) + else: + gate = super().control(num_ctrl_qubits, label=label, ctrl_state=ctrl_state) return gate diff --git a/qiskit/circuit/library/standard_gates/y.py b/qiskit/circuit/library/standard_gates/y.py index f2a3aab87522..82fb1179b6f3 100644 --- a/qiskit/circuit/library/standard_gates/y.py +++ b/qiskit/circuit/library/standard_gates/y.py @@ -17,7 +17,6 @@ # pylint: disable=cyclic-import from qiskit.circuit.singleton import SingletonGate, SingletonControlledGate, stdlib_singleton_key -from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier from qiskit.circuit.quantumregister import QuantumRegister from qiskit.circuit._utils import with_gate_array, with_controlled_gate_array @@ -112,19 +111,14 @@ def control( Returns: ControlledGate: controlled version of this gate. """ - if not annotated: - if num_ctrl_qubits == 1: - gate = CYGate(label=label, ctrl_state=ctrl_state, _base_label=self.label) - else: - gate = super().control( - num_ctrl_qubits=num_ctrl_qubits, - label=label, - ctrl_state=ctrl_state, - annotated=annotated, - ) + if not annotated and num_ctrl_qubits == 1: + gate = CYGate(label=label, ctrl_state=ctrl_state, _base_label=self.label) else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate diff --git a/qiskit/circuit/library/standard_gates/z.py b/qiskit/circuit/library/standard_gates/z.py index 15d862e54d77..f719b13807f3 100644 --- a/qiskit/circuit/library/standard_gates/z.py +++ b/qiskit/circuit/library/standard_gates/z.py @@ -19,7 +19,6 @@ from qiskit.circuit._utils import with_gate_array, with_controlled_gate_array from qiskit.circuit.singleton import SingletonGate, SingletonControlledGate, stdlib_singleton_key -from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier from qiskit.circuit.quantumregister import QuantumRegister from .p import PhaseGate @@ -116,19 +115,14 @@ def control( Returns: ControlledGate: controlled version of this gate. """ - if not annotated: - if num_ctrl_qubits == 1: - gate = CZGate(label=label, ctrl_state=ctrl_state, _base_label=self.label) - else: - gate = super().control( - num_ctrl_qubits=num_ctrl_qubits, - label=label, - ctrl_state=ctrl_state, - annotated=annotated, - ) + if not annotated and num_ctrl_qubits == 1: + gate = CZGate(label=label, ctrl_state=ctrl_state, _base_label=self.label) else: - gate = AnnotatedOperation( - self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state) + gate = super().control( + num_ctrl_qubits=num_ctrl_qubits, + label=label, + ctrl_state=ctrl_state, + annotated=annotated, ) return gate