Skip to content

Commit 0783fbb

Browse files
simplifying logic based on code review
1 parent 268b265 commit 0783fbb

File tree

14 files changed

+174
-257
lines changed

14 files changed

+174
-257
lines changed

qiskit/circuit/library/generalized_gates/mcmt.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
from qiskit import circuit
2020
from qiskit.circuit import ControlledGate, Gate, QuantumRegister, QuantumCircuit
21-
from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier
2221
from qiskit.exceptions import QiskitError
2322

2423
# pylint: disable=cyclic-import
@@ -145,17 +144,10 @@ def _identify_gate(self, gate):
145144

146145
def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None, annotated=False):
147146
"""Return the controlled version of the MCMT circuit."""
148-
if not annotated:
149-
if ctrl_state is None: # TODO add ctrl state implementation by adding X gates
150-
gate = MCMT(
151-
self.gate, self.num_ctrl_qubits + num_ctrl_qubits, self.num_target_qubits
152-
)
153-
else:
154-
gate = super().control(num_ctrl_qubits, label, ctrl_state, annotated=annotated)
147+
if not annotated and ctrl_state is None:
148+
gate = MCMT(self.gate, self.num_ctrl_qubits + num_ctrl_qubits, self.num_target_qubits)
155149
else:
156-
gate = AnnotatedOperation(
157-
self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state)
158-
)
150+
gate = super().control(num_ctrl_qubits, label, ctrl_state, annotated=annotated)
159151
return gate
160152

161153
def inverse(self):

qiskit/circuit/library/standard_gates/h.py

+7-13
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from typing import Optional, Union
1616
import numpy
1717
from qiskit.circuit.singleton import SingletonGate, SingletonControlledGate, stdlib_singleton_key
18-
from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier
1918
from qiskit.circuit.quantumregister import QuantumRegister
2019
from qiskit.circuit._utils import with_gate_array, with_controlled_gate_array
2120

@@ -96,19 +95,14 @@ def control(
9695
Returns:
9796
ControlledGate: controlled version of this gate.
9897
"""
99-
if not annotated:
100-
if num_ctrl_qubits == 1:
101-
gate = CHGate(label=label, ctrl_state=ctrl_state, _base_label=self.label)
102-
else:
103-
gate = super().control(
104-
num_ctrl_qubits=num_ctrl_qubits,
105-
label=label,
106-
ctrl_state=ctrl_state,
107-
annotated=annotated,
108-
)
98+
if not annotated and num_ctrl_qubits == 1:
99+
gate = CHGate(label=label, ctrl_state=ctrl_state, _base_label=self.label)
109100
else:
110-
gate = AnnotatedOperation(
111-
self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state)
101+
gate = super().control(
102+
num_ctrl_qubits=num_ctrl_qubits,
103+
label=label,
104+
ctrl_state=ctrl_state,
105+
annotated=annotated,
112106
)
113107
return gate
114108

qiskit/circuit/library/standard_gates/p.py

+31-47
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from cmath import exp
1717
import numpy
1818
from qiskit.circuit.controlledgate import ControlledGate
19-
from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier
2019
from qiskit.circuit.gate import Gate
2120
from qiskit.circuit.quantumregister import QuantumRegister
2221
from qiskit.circuit.parameterexpression import ParameterValueType
@@ -112,23 +111,18 @@ def control(
112111
Returns:
113112
ControlledGate: controlled version of this gate.
114113
"""
115-
if not annotated:
116-
if num_ctrl_qubits == 1:
117-
gate = CPhaseGate(self.params[0], label=label, ctrl_state=ctrl_state)
118-
gate.base_gate.label = self.label
119-
elif ctrl_state is None and num_ctrl_qubits > 1:
120-
gate = MCPhaseGate(self.params[0], num_ctrl_qubits, label=label)
121-
gate.base_gate.label = self.label
122-
else:
123-
gate = super().control(
124-
num_ctrl_qubits=num_ctrl_qubits,
125-
label=label,
126-
ctrl_state=ctrl_state,
127-
annotated=annotated,
128-
)
114+
if not annotated and num_ctrl_qubits == 1:
115+
gate = CPhaseGate(self.params[0], label=label, ctrl_state=ctrl_state)
116+
gate.base_gate.label = self.label
117+
elif not annotated and ctrl_state is None and num_ctrl_qubits > 1:
118+
gate = MCPhaseGate(self.params[0], num_ctrl_qubits, label=label)
119+
gate.base_gate.label = self.label
129120
else:
130-
gate = AnnotatedOperation(
131-
self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state)
121+
gate = super().control(
122+
num_ctrl_qubits=num_ctrl_qubits,
123+
label=label,
124+
ctrl_state=ctrl_state,
125+
annotated=annotated,
132126
)
133127
return gate
134128

@@ -255,20 +249,15 @@ def control(
255249
Returns:
256250
ControlledGate: controlled version of this gate.
257251
"""
258-
if not annotated:
259-
if ctrl_state is None:
260-
gate = MCPhaseGate(self.params[0], num_ctrl_qubits=num_ctrl_qubits + 1, label=label)
261-
gate.base_gate.label = self.label
262-
else:
263-
gate = super().control(
264-
num_ctrl_qubits=num_ctrl_qubits,
265-
label=label,
266-
ctrl_state=ctrl_state,
267-
annotated=annotated,
268-
)
252+
if not annotated and ctrl_state is None:
253+
gate = MCPhaseGate(self.params[0], num_ctrl_qubits=num_ctrl_qubits + 1, label=label)
254+
gate.base_gate.label = self.label
269255
else:
270-
gate = AnnotatedOperation(
271-
self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state)
256+
gate = super().control(
257+
num_ctrl_qubits=num_ctrl_qubits,
258+
label=label,
259+
ctrl_state=ctrl_state,
260+
annotated=annotated,
272261
)
273262
return gate
274263

@@ -381,24 +370,19 @@ def control(
381370
Returns:
382371
ControlledGate: controlled version of this gate.
383372
"""
384-
if not annotated:
385-
if ctrl_state is None:
386-
gate = MCPhaseGate(
387-
self.params[0],
388-
num_ctrl_qubits=num_ctrl_qubits + self.num_ctrl_qubits,
389-
label=label,
390-
)
391-
gate.base_gate.label = self.label
392-
else:
393-
gate = super().control(
394-
num_ctrl_qubits=num_ctrl_qubits,
395-
label=label,
396-
ctrl_state=ctrl_state,
397-
annotated=annotated,
398-
)
373+
if not annotated and ctrl_state is None:
374+
gate = MCPhaseGate(
375+
self.params[0],
376+
num_ctrl_qubits=num_ctrl_qubits + self.num_ctrl_qubits,
377+
label=label,
378+
)
379+
gate.base_gate.label = self.label
399380
else:
400-
gate = AnnotatedOperation(
401-
self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state)
381+
gate = super().control(
382+
num_ctrl_qubits=num_ctrl_qubits,
383+
label=label,
384+
ctrl_state=ctrl_state,
385+
annotated=annotated,
402386
)
403387
return gate
404388

qiskit/circuit/library/standard_gates/rx.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import numpy
1919

2020
from qiskit.circuit.controlledgate import ControlledGate
21-
from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier
2221
from qiskit.circuit.gate import Gate
2322
from qiskit.circuit.quantumregister import QuantumRegister
2423
from qiskit.circuit.parameterexpression import ParameterValueType
@@ -93,20 +92,15 @@ def control(
9392
Returns:
9493
ControlledGate: controlled version of this gate.
9594
"""
96-
if not annotated:
97-
if num_ctrl_qubits == 1:
98-
gate = CRXGate(self.params[0], label=label, ctrl_state=ctrl_state)
99-
gate.base_gate.label = self.label
100-
else:
101-
gate = super().control(
102-
num_ctrl_qubits=num_ctrl_qubits,
103-
label=label,
104-
ctrl_state=ctrl_state,
105-
annotated=annotated,
106-
)
95+
if not annotated and num_ctrl_qubits == 1:
96+
gate = CRXGate(self.params[0], label=label, ctrl_state=ctrl_state)
97+
gate.base_gate.label = self.label
10798
else:
108-
gate = AnnotatedOperation(
109-
self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state)
99+
gate = super().control(
100+
num_ctrl_qubits=num_ctrl_qubits,
101+
label=label,
102+
ctrl_state=ctrl_state,
103+
annotated=annotated,
110104
)
111105
return gate
112106

qiskit/circuit/library/standard_gates/ry.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from typing import Optional, Union
1818
import numpy
1919
from qiskit.circuit.controlledgate import ControlledGate
20-
from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier
2120
from qiskit.circuit.gate import Gate
2221
from qiskit.circuit.quantumregister import QuantumRegister
2322
from qiskit.circuit.parameterexpression import ParameterValueType
@@ -92,20 +91,15 @@ def control(
9291
Returns:
9392
ControlledGate: controlled version of this gate.
9493
"""
95-
if not annotated:
96-
if num_ctrl_qubits == 1:
97-
gate = CRYGate(self.params[0], label=label, ctrl_state=ctrl_state)
98-
gate.base_gate.label = self.label
99-
else:
100-
gate = super().control(
101-
num_ctrl_qubits=num_ctrl_qubits,
102-
label=label,
103-
ctrl_state=ctrl_state,
104-
annotated=annotated,
105-
)
94+
if not annotated and num_ctrl_qubits == 1:
95+
gate = CRYGate(self.params[0], label=label, ctrl_state=ctrl_state)
96+
gate.base_gate.label = self.label
10697
else:
107-
gate = AnnotatedOperation(
108-
self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state)
98+
gate = super().control(
99+
num_ctrl_qubits=num_ctrl_qubits,
100+
label=label,
101+
ctrl_state=ctrl_state,
102+
annotated=annotated,
109103
)
110104
return gate
111105

qiskit/circuit/library/standard_gates/rz.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from typing import Optional, Union
1616
from qiskit.circuit.gate import Gate
1717
from qiskit.circuit.controlledgate import ControlledGate
18-
from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier
1918
from qiskit.circuit.quantumregister import QuantumRegister
2019
from qiskit.circuit.parameterexpression import ParameterValueType
2120

@@ -103,20 +102,15 @@ def control(
103102
Returns:
104103
ControlledGate: controlled version of this gate.
105104
"""
106-
if not annotated:
107-
if num_ctrl_qubits == 1:
108-
gate = CRZGate(self.params[0], label=label, ctrl_state=ctrl_state)
109-
gate.base_gate.label = self.label
110-
else:
111-
gate = super().control(
112-
num_ctrl_qubits=num_ctrl_qubits,
113-
label=label,
114-
ctrl_state=ctrl_state,
115-
annotated=annotated,
116-
)
105+
if not annotated and num_ctrl_qubits == 1:
106+
gate = CRZGate(self.params[0], label=label, ctrl_state=ctrl_state)
107+
gate.base_gate.label = self.label
117108
else:
118-
gate = AnnotatedOperation(
119-
self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state)
109+
gate = super().control(
110+
num_ctrl_qubits=num_ctrl_qubits,
111+
label=label,
112+
ctrl_state=ctrl_state,
113+
annotated=annotated,
120114
)
121115
return gate
122116

qiskit/circuit/library/standard_gates/swap.py

+7-13
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from typing import Optional, Union
1616
import numpy
1717
from qiskit.circuit.singleton import SingletonGate, SingletonControlledGate, stdlib_singleton_key
18-
from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier
1918
from qiskit.circuit.quantumregister import QuantumRegister
2019
from qiskit.circuit._utils import with_gate_array, with_controlled_gate_array
2120

@@ -107,19 +106,14 @@ def control(
107106
Returns:
108107
ControlledGate: controlled version of this gate.
109108
"""
110-
if not annotated:
111-
if num_ctrl_qubits == 1:
112-
gate = CSwapGate(label=label, ctrl_state=ctrl_state, _base_label=self.label)
113-
else:
114-
gate = super().control(
115-
num_ctrl_qubits=num_ctrl_qubits,
116-
label=label,
117-
ctrl_state=ctrl_state,
118-
annotated=annotated,
119-
)
109+
if not annotated and num_ctrl_qubits == 1:
110+
gate = CSwapGate(label=label, ctrl_state=ctrl_state, _base_label=self.label)
120111
else:
121-
gate = AnnotatedOperation(
122-
self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state)
112+
gate = super().control(
113+
num_ctrl_qubits=num_ctrl_qubits,
114+
label=label,
115+
ctrl_state=ctrl_state,
116+
annotated=annotated,
123117
)
124118
return gate
125119

qiskit/circuit/library/standard_gates/sx.py

+7-13
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from math import pi
1616
from typing import Optional, Union
1717
from qiskit.circuit.singleton import SingletonGate, SingletonControlledGate, stdlib_singleton_key
18-
from qiskit.circuit.annotated_operation import AnnotatedOperation, ControlModifier
1918
from qiskit.circuit.quantumregister import QuantumRegister
2019
from qiskit.circuit._utils import with_gate_array, with_controlled_gate_array
2120

@@ -111,19 +110,14 @@ def control(
111110
Returns:
112111
SingletonControlledGate: controlled version of this gate.
113112
"""
114-
if not annotated:
115-
if num_ctrl_qubits == 1:
116-
gate = CSXGate(label=label, ctrl_state=ctrl_state, _base_label=self.label)
117-
else:
118-
gate = super().control(
119-
num_ctrl_qubits=num_ctrl_qubits,
120-
label=label,
121-
ctrl_state=ctrl_state,
122-
annotated=annotated,
123-
)
113+
if not annotated and num_ctrl_qubits == 1:
114+
gate = CSXGate(label=label, ctrl_state=ctrl_state, _base_label=self.label)
124115
else:
125-
gate = AnnotatedOperation(
126-
self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state)
116+
gate = super().control(
117+
num_ctrl_qubits=num_ctrl_qubits,
118+
label=label,
119+
ctrl_state=ctrl_state,
120+
annotated=annotated,
127121
)
128122
return gate
129123

0 commit comments

Comments
 (0)