-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a207fdf
commit fd06033
Showing
5 changed files
with
202 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# Copyright 2023 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 Dict, Optional, Set, Tuple, TYPE_CHECKING, Union | ||
|
||
from attrs import frozen | ||
|
||
from qualtran import Bloq, Register, Signature | ||
from qualtran.bloqs.basic_gates import TGate | ||
|
||
if TYPE_CHECKING: | ||
import cirq | ||
|
||
from qualtran.cirq_interop import CirqQuregT | ||
from qualtran.resource_counting import BloqCountT, SympySymbolAllocator | ||
from qualtran.simulation.classical_sim import ClassicalValT | ||
|
||
|
||
@frozen | ||
class Toffoli(Bloq): | ||
"""The Toffoli gate. | ||
This will flip the target bit if both controls are active. It can be thought of as | ||
a reversible AND gate. | ||
Like `TGate`, this is a common compilation target. The Clifford+Toffoli gateset is | ||
universal. | ||
References: | ||
[Novel constructions for the fault-tolerant Toffoli gate](https://arxiv.org/abs/1212.5069). | ||
Cody Jones. 2012. Provides a decomposition into 4 `TGate`. | ||
""" | ||
|
||
@cached_property | ||
def signature(self) -> Signature: | ||
return Signature([Register('ctrl', 1, shape=(2,)), Register('target', 1)]) | ||
|
||
def bloq_counts(self, ssa: Optional['SympySymbolAllocator'] = None) -> Set['BloqCountT']: | ||
return {(4, TGate())} | ||
|
||
def on_classical_vals( | ||
self, ctrl: 'ClassicalValT', target: 'ClassicalValT' | ||
) -> Dict[str, 'ClassicalValT']: | ||
assert target in [0, 1] | ||
if ctrl[0] == 1 and ctrl[1] == 1: | ||
target = (target + 1) % 2 | ||
|
||
return {'ctrl': ctrl, 'target': target} | ||
|
||
def as_cirq_op( | ||
self, qubit_manager: 'cirq.QubitManager', ctrl: 'CirqQuregT', target: 'CirqQuregT' | ||
) -> Tuple[Union['cirq.Operation', None], Dict[str, 'CirqQuregT']]: | ||
import cirq | ||
|
||
(trg,) = target | ||
return cirq.CCNOT(*ctrl[:, 0], trg), {'ctrl': ctrl, 'target': target} |
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,78 @@ | ||
# Copyright 2023 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 itertools | ||
|
||
import cirq | ||
|
||
from qualtran import BloqBuilder | ||
from qualtran.bloqs.basic_gates import TGate, Toffoli, ZeroState | ||
from qualtran.resource_counting import get_bloq_counts_graph | ||
|
||
|
||
def _make_Toffoli(): | ||
from qualtran.bloqs.basic_gates import Toffoli | ||
|
||
return Toffoli() | ||
|
||
|
||
def test_toffoli_t_count(): | ||
counts = Toffoli().bloq_counts() | ||
assert counts == {(4, TGate())} | ||
|
||
_, sigma = get_bloq_counts_graph(Toffoli()) | ||
assert sigma == {TGate(): 4} | ||
|
||
|
||
def test_toffoli_cirq(): | ||
bb = BloqBuilder() | ||
c0, c1, trg = [bb.add(ZeroState()) for _ in range(3)] | ||
ctrl, target = bb.add(Toffoli(), ctrl=[c0, c1], target=trg) | ||
ctrl, target = bb.add(Toffoli(), ctrl=ctrl, target=target) | ||
cbloq = bb.finalize(q0=ctrl[0], q1=ctrl[1], q2=target) | ||
|
||
circuit, qubits = cbloq.to_cirq_circuit() | ||
cirq.testing.assert_has_diagram( | ||
circuit, | ||
"""\ | ||
_c(0): ───@───@─── | ||
│ │ | ||
_c(1): ───@───@─── | ||
│ │ | ||
_c(2): ───X───X───""", | ||
) | ||
|
||
|
||
def test_classical_sim(): | ||
tof = Toffoli() | ||
|
||
for c0, c1 in itertools.product([0, 1], repeat=2): | ||
ctrl, target = tof.call_classically(ctrl=[c0, c1], target=0) | ||
assert ctrl.tolist() == [c0, c1] | ||
if c0 == 1 and c1 == 1: | ||
assert target == 1 | ||
else: | ||
assert target == 0 | ||
|
||
|
||
def test_classical_sim_2(): | ||
bb = BloqBuilder() | ||
c0, c1, trg = [bb.add(ZeroState()) for _ in range(3)] | ||
ctrl, target = bb.add(Toffoli(), ctrl=[c0, c1], target=trg) | ||
ctrl, target = bb.add(Toffoli(), ctrl=ctrl, target=target) | ||
cbloq = bb.finalize(q0=ctrl[0], q1=ctrl[1], q2=target) | ||
|
||
b0, b1, b2 = cbloq.call_classically() | ||
assert b0 == 0 | ||
assert b1 == 0 | ||
assert b2 == 0 |