Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unitary validation check when constructing GateWithRegisters.controlled() #887

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions qualtran/_infra/gate_with_registers.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,39 @@ def _get_all_and_output_quregs_from_input(
return all_quregs, out_quregs


def _get_cirq_cv(
num_controls: Optional[int] = None,
control_values=None,
control_qid_shape: Optional[Tuple[int, ...]] = None,
) -> cirq.ops.AbstractControlValues:
"""Logic copied from `cirq.ControlledGate` to help convert cirq-style spec to `CtrlSpec`"""
if isinstance(control_values, cirq.SumOfProducts) and len(control_values._conjunctions) == 1:
control_values = control_values._conjunctions[0]
if num_controls is None:
if control_values is not None:
num_controls = (
control_values._num_qubits_()
if isinstance(control_values, cirq.ops.AbstractControlValues)
else len(control_values)
)
elif control_qid_shape is not None:
num_controls = len(control_qid_shape)
else:
num_controls = 1
if control_values is None:
control_values = ((1,),) * num_controls
if not isinstance(control_values, cirq.ops.AbstractControlValues):
control_values = cirq.ProductOfSums(control_values)
if num_controls != cirq.num_qubits(control_values):
raise ValueError('cirq.num_qubits(control_values) != num_controls')
if control_qid_shape is None:
control_qid_shape = (2,) * num_controls
if num_controls != len(control_qid_shape):
raise ValueError('len(control_qid_shape) != num_controls')
control_values.validate(control_qid_shape)
return control_values


class GateWithRegisters(Bloq, cirq.Gate, metaclass=abc.ABCMeta):
"""`cirq.Gate`s extension with support for composite gates acting on multiple qubit registers.

Expand Down Expand Up @@ -394,13 +427,12 @@ def _get_ctrl_spec(
if isinstance(num_controls, CtrlSpec):
ctrl_spec = num_controls
elif ctrl_spec is None:
controlled_gate = cirq.ControlledGate(
self,
control_values = _get_cirq_cv(
num_controls=num_controls,
control_values=control_values,
control_qid_shape=control_qid_shape,
)
ctrl_spec = CtrlSpec.from_cirq_cv(controlled_gate.control_values)
ctrl_spec = CtrlSpec.from_cirq_cv(control_values)
return ctrl_spec

# pylint: disable=arguments-renamed
Expand Down
5 changes: 5 additions & 0 deletions qualtran/_infra/gate_with_registers_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ def test_gate_with_registers_decompose_from_context_auto_generated():
)


def test_non_unitary_controlled():
bloq = BloqWithDecompose()
assert bloq.controlled(control_values=[0]) == Controlled(bloq, CtrlSpec(cvs=0))


@pytest.mark.notebook
def test_notebook():
execute_notebook('gate_with_registers')
Loading