Skip to content

Commit

Permalink
simplifying logic based on code review
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderivrii committed Jan 31, 2024
1 parent 268b265 commit 0783fbb
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 257 deletions.
14 changes: 3 additions & 11 deletions qiskit/circuit/library/generalized_gates/mcmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
20 changes: 7 additions & 13 deletions qiskit/circuit/library/standard_gates/h.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
78 changes: 31 additions & 47 deletions qiskit/circuit/library/standard_gates/p.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
22 changes: 8 additions & 14 deletions qiskit/circuit/library/standard_gates/rx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
22 changes: 8 additions & 14 deletions qiskit/circuit/library/standard_gates/ry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
22 changes: 8 additions & 14 deletions qiskit/circuit/library/standard_gates/rz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
20 changes: 7 additions & 13 deletions qiskit/circuit/library/standard_gates/swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
20 changes: 7 additions & 13 deletions qiskit/circuit/library/standard_gates/sx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 0783fbb

Please sign in to comment.