Skip to content

Commit

Permalink
Skip validation on gate creation from rust
Browse files Browse the repository at this point in the history
  • Loading branch information
mtreinish committed May 31, 2024
1 parent 3ea95de commit 2f81bde
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 16 deletions.
14 changes: 11 additions & 3 deletions crates/circuit/src/circuit_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,12 +626,20 @@ pub(crate) fn operation_type_and_data_to_py(
} else {
PyTuple::new_bound(py, params)
};
let kwargs = [
let mut kwargs_list = vec![
("label", label.to_object(py)),
("unit", unit.to_object(py)),
("duration", duration.to_object(py)),
]
.into_py_dict_bound(py);
];
if let Some(params) = params {
if !params.is_empty() {
kwargs_list.push(
("_skip_validation", true.to_object(py))
);
}
}

let kwargs = kwargs_list.into_py_dict_bound(py);
let mut out = gate_class.call_bound(py, args, Some(&kwargs))?;
if condition.is_some() {
out = out.call_method0(py, "to_mutable")?;
Expand Down
7 changes: 6 additions & 1 deletion qiskit/circuit/gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from qiskit.circuit.parameterexpression import ParameterExpression
from qiskit.circuit.exceptions import CircuitError
from qiskit._accelerate.circuit import StandardGate
from .annotated_operation import AnnotatedOperation, ControlModifier, PowerModifier
from .instruction import Instruction

Expand All @@ -33,6 +34,7 @@ def __init__(
label: str | None = None,
duration=None,
unit="dt",
_skip_validation=False,
) -> None:
"""Create a new gate.
Expand All @@ -42,6 +44,7 @@ def __init__(
params: A list of parameters.
label: An optional label for the gate.
"""
self._skip_validation = _skip_validation
self.definition = None
super().__init__(name, num_qubits, 0, params, label=label, duration=duration, unit=unit)

Expand Down Expand Up @@ -238,7 +241,9 @@ def broadcast_arguments(self, qargs: list, cargs: list) -> Iterable[tuple[list,
else:
raise CircuitError("This gate cannot handle %i arguments" % len(qargs))

def validate_parameter(self, parameter):
def validate_parameter(self, parameter, _force_validation=False):
if (isinstance(self, StandardGate) or self._skip_validation) and not _force_validation:
return parameter
"""Gate parameters should be int, float, or ParameterExpression"""
if isinstance(parameter, ParameterExpression):
if len(parameter.parameters) > 0:
Expand Down
18 changes: 16 additions & 2 deletions qiskit/circuit/library/standard_gates/global_phase.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,28 @@ class GlobalPhaseGate(Gate):
_standard_gate = StandardGate.GlobalPhaseGate

def __init__(
self, phase: ParameterValueType, label: Optional[str] = None, *, duration=None, unit="dt"
self,
phase: ParameterValueType,
label: Optional[str] = None,
*,
duration=None,
unit="dt",
_skip_validation=False,
):
"""
Args:
phase: The value of phase it takes.
label: An optional label for the gate.
"""
super().__init__("global_phase", 0, [phase], label=label, duration=duration, unit=unit)
super().__init__(
"global_phase",
0,
[phase],
label=label,
duration=duration,
unit=unit,
_skip_validation=_skip_validation,
)

def _define(self):
q = QuantumRegister(0, "q")
Expand Down
18 changes: 16 additions & 2 deletions qiskit/circuit/library/standard_gates/p.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,24 @@ class PhaseGate(Gate):
_standard_gate = StandardGate.PhaseGate

def __init__(
self, theta: ParameterValueType, label: str | None = None, *, duration=None, unit="dt"
self,
theta: ParameterValueType,
label: str | None = None,
*,
duration=None,
unit="dt",
_skip_validation=False,
):
"""Create new Phase gate."""
super().__init__("p", 1, [theta], label=label, duration=duration, unit=unit)
super().__init__(
"p",
1,
[theta],
label=label,
duration=duration,
unit=unit,
_skip_validation=_skip_validation,
)

def _define(self):
# pylint: disable=cyclic-import
Expand Down
18 changes: 16 additions & 2 deletions qiskit/circuit/library/standard_gates/rx.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,24 @@ class RXGate(Gate):
_standard_gate = StandardGate.RXGate

def __init__(
self, theta: ParameterValueType, label: Optional[str] = None, *, duration=None, unit="dt"
self,
theta: ParameterValueType,
label: Optional[str] = None,
*,
duration=None,
unit="dt",
_skip_validation=False,
):
"""Create new RX gate."""
super().__init__("rx", 1, [theta], label=label, duration=duration, unit=unit)
super().__init__(
"rx",
1,
[theta],
label=label,
duration=duration,
unit=unit,
_skip_validation=_skip_validation,
)

def _define(self):
"""
Expand Down
18 changes: 16 additions & 2 deletions qiskit/circuit/library/standard_gates/ry.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,24 @@ class RYGate(Gate):
_standard_gate = StandardGate.RYGate

def __init__(
self, theta: ParameterValueType, label: Optional[str] = None, *, duration=None, unit="dt"
self,
theta: ParameterValueType,
label: Optional[str] = None,
*,
duration=None,
unit="dt",
_skip_validation=False,
):
"""Create new RY gate."""
super().__init__("ry", 1, [theta], label=label, duration=duration, unit=unit)
super().__init__(
"ry",
1,
[theta],
label=label,
duration=duration,
unit=unit,
_skip_validation=_skip_validation,
)

def _define(self):
"""
Expand Down
18 changes: 16 additions & 2 deletions qiskit/circuit/library/standard_gates/rz.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,24 @@ class RZGate(Gate):
_standard_gate = StandardGate.RZGate

def __init__(
self, phi: ParameterValueType, label: Optional[str] = None, *, duration=None, unit="dt"
self,
phi: ParameterValueType,
label: Optional[str] = None,
*,
duration=None,
unit="dt",
_skip_validation=False,
):
"""Create new RZ gate."""
super().__init__("rz", 1, [phi], label=label, duration=duration, unit=unit)
super().__init__(
"rz",
1,
[phi],
label=label,
duration=duration,
unit=unit,
_skip_validation=_skip_validation,
)

def _define(self):
"""
Expand Down
11 changes: 10 additions & 1 deletion qiskit/circuit/library/standard_gates/u.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,18 @@ def __init__(
*,
duration=None,
unit="dt",
_skip_validation=False,
):
"""Create new U gate."""
super().__init__("u", 1, [theta, phi, lam], label=label, duration=duration, unit=unit)
super().__init__(
"u",
1,
[theta, phi, lam],
label=label,
duration=duration,
unit=unit,
_skip_validation=_skip_validation,
)

def inverse(self, annotated: bool = False):
r"""Return inverted U gate.
Expand Down
2 changes: 1 addition & 1 deletion qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2317,7 +2317,7 @@ def _append_standard_gate(
expanded_cargs = [self.cbit_argument_conversion(carg) for carg in cargs or []]
if params is not None:
for param in params:
Gate.validate_parameter(op, param)
Gate.validate_parameter(op, param, _force_validation=True)

instructions = InstructionSet(resource_requester=circuit_scope.resolve_classical_resource)
broadcast_iter = Gate.broadcast_arguments(op, expanded_qargs, expanded_cargs)
Expand Down

0 comments on commit 2f81bde

Please sign in to comment.