-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
MultiTargetCNOT
to separate file (#1262)
* Move `MultiTargetCNOT` to separate file * auto test bloqs
- Loading branch information
Showing
11 changed files
with
133 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from functools import cached_property | ||
from typing import Iterator | ||
|
||
import cirq | ||
import sympy | ||
from attrs import frozen | ||
from numpy.typing import NDArray | ||
|
||
from qualtran import bloq_example, BloqDocSpec, GateWithRegisters, Signature | ||
from qualtran.symbolics import SymbolicInt | ||
|
||
|
||
@frozen | ||
class MultiTargetCNOT(GateWithRegisters): | ||
r"""Implements single control, multi-target $C[X^{\otimes n}]$ gate. | ||
Implements $|0><0| I + |1><1| X^{\otimes n}$ using a circuit of depth $2\log(n) + 1$ | ||
containing only CNOT gates. | ||
References: | ||
[Trading T-gates for dirty qubits in state preparation and unitary synthesis](https://arxiv.org/abs/1812.00954). | ||
Appendix B.1. | ||
""" | ||
|
||
bitsize: 'SymbolicInt' | ||
|
||
@cached_property | ||
def signature(self) -> Signature: | ||
return Signature.build(control=1, targets=self.bitsize) | ||
|
||
def decompose_from_registers( | ||
self, | ||
*, | ||
context: cirq.DecompositionContext, | ||
control: NDArray[cirq.Qid], # type: ignore[type-var] | ||
targets: NDArray[cirq.Qid], # type: ignore[type-var] | ||
): | ||
def cnots_for_depth_i(i: int, q: NDArray[cirq.Qid]) -> Iterator[cirq.OP_TREE]: # type: ignore[type-var] | ||
for c, t in zip(q[: 2**i], q[2**i : min(len(q), 2 ** (i + 1))]): | ||
yield cirq.CNOT(c, t) | ||
|
||
depth = len(targets).bit_length() | ||
for i in range(depth): | ||
yield cirq.Moment(cnots_for_depth_i(depth - i - 1, targets)) | ||
yield cirq.CNOT(*control, targets[0]) | ||
for i in range(depth): | ||
yield cirq.Moment(cnots_for_depth_i(i, targets)) | ||
|
||
def _circuit_diagram_info_(self, _) -> cirq.CircuitDiagramInfo: | ||
if isinstance(self.bitsize, sympy.Expr): | ||
raise ValueError(f'Symbolic bitsize {self.bitsize} not supported') | ||
return cirq.CircuitDiagramInfo(wire_symbols=["@"] + ["X"] * self.bitsize) | ||
|
||
|
||
@bloq_example | ||
def _c_multi_not_symb() -> MultiTargetCNOT: | ||
n = sympy.Symbol('n') | ||
c_multi_not_symb = MultiTargetCNOT(bitsize=n) | ||
return c_multi_not_symb | ||
|
||
|
||
@bloq_example | ||
def _c_multi_not() -> MultiTargetCNOT: | ||
c_multi_not = MultiTargetCNOT(bitsize=5) | ||
return c_multi_not | ||
|
||
|
||
_C_MULTI_NOT_DOC = BloqDocSpec(bloq_cls=MultiTargetCNOT, examples=(_c_multi_not_symb, _c_multi_not)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import cirq | ||
import numpy as np | ||
import pytest | ||
|
||
import qualtran.testing as qlt_testing | ||
from qualtran.bloqs.mcmt.multi_target_cnot import _c_multi_not, _c_multi_not_symb, MultiTargetCNOT | ||
|
||
|
||
def test_examples(bloq_autotester): | ||
bloq_autotester(_c_multi_not) | ||
|
||
|
||
def test_symbolic_examples(bloq_autotester): | ||
bloq_autotester(_c_multi_not_symb) | ||
|
||
|
||
@pytest.mark.parametrize("num_targets", [3, 4, 6, 8, 10]) | ||
def test_multi_target_cnot(num_targets): | ||
qubits = cirq.LineQubit.range(num_targets + 1) | ||
naive_circuit = cirq.Circuit(cirq.CNOT(qubits[0], q) for q in qubits[1:]) | ||
bloq = MultiTargetCNOT(num_targets) | ||
op = bloq.on(*qubits) | ||
cirq.testing.assert_circuits_with_terminal_measurements_are_equivalent( | ||
cirq.Circuit(op), naive_circuit, atol=1e-6 | ||
) | ||
optimal_circuit = cirq.Circuit(cirq.decompose_once(op)) | ||
assert len(optimal_circuit) == 2 * np.ceil(np.log2(num_targets)) + 1 | ||
qlt_testing.assert_valid_bloq_decomposition(bloq) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters