Skip to content

Commit

Permalink
move qiskit.circuit.add_control._add_control
Browse files Browse the repository at this point in the history
  • Loading branch information
1ucian0 committed Sep 26, 2024
1 parent 5c78bc8 commit 0b3266d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 46 deletions.
43 changes: 0 additions & 43 deletions qiskit/circuit/add_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from __future__ import annotations

from qiskit.circuit.exceptions import CircuitError
from qiskit.circuit.library import UnitaryGate
from qiskit.transpiler import PassManager
from qiskit.transpiler.passes.basis import BasisTranslator, UnrollCustomDefinitions
from qiskit.circuit.equivalence_library import SessionEquivalenceLibrary as sel
Expand All @@ -23,48 +22,6 @@
from ._utils import _ctrl_state_to_int


def _add_control(
operation: Gate | ControlledGate,
num_ctrl_qubits: int,
label: str | None,
ctrl_state: str | int | None,
) -> ControlledGate:
"""For standard gates, if the controlled version already exists in the
library, it will be returned (e.g. XGate.control() = CnotGate().
For more generic gates, this method implements the controlled
version by first decomposing into the ['u1', 'u3', 'cx'] basis, then
controlling each gate in the decomposition.
Open controls are implemented by conjugating the control line with
X gates. Adds num_ctrl_qubits controls to operation.
This function is meant to be called from the
:method:`qiskit.circuit.gate.Gate.control()` method.
Args:
operation: The operation to be controlled.
num_ctrl_qubits: The number of controls to add to gate.
label: An optional gate label.
ctrl_state: The control state in decimal or as a bitstring
(e.g. '111'). If specified as a bitstring the length
must equal num_ctrl_qubits, MSB on left. If None, use
2**num_ctrl_qubits-1.
Returns:
Controlled version of gate.
"""
if isinstance(operation, UnitaryGate):
# attempt decomposition
operation._define()
cgate = control(operation, num_ctrl_qubits=num_ctrl_qubits, label=label, ctrl_state=ctrl_state)
if operation.label is not None:
cgate.base_gate = cgate.base_gate.to_mutable()
cgate.base_gate.label = operation.label
return cgate


def control(
operation: Gate | ControlledGate,
num_ctrl_qubits: int | None = 1,
Expand Down
11 changes: 8 additions & 3 deletions qiskit/circuit/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,15 @@ def control(
"""
if not annotated: # captures both None and False
# pylint: disable=cyclic-import
from .add_control import _add_control

return _add_control(self, num_ctrl_qubits, label, ctrl_state)
from .add_control import control

cgate = control(
self, num_ctrl_qubits=num_ctrl_qubits, label=label, ctrl_state=ctrl_state
)
if self.label is not None:
cgate.base_gate = cgate.base_gate.to_mutable()
cgate.base_gate.label = self.label
return cgate
else:
return AnnotatedOperation(
self, ControlModifier(num_ctrl_qubits=num_ctrl_qubits, ctrl_state=ctrl_state)
Expand Down

0 comments on commit 0b3266d

Please sign in to comment.