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

Decomposition of TOFFOLI into CNOT, T, TDG, and H #1355

Merged
merged 4 commits into from
Jun 12, 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
20 changes: 20 additions & 0 deletions src/qibo/transpiler/decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,23 @@ def _u3_to_gpi2(t, p, l):
gates.ECR, [gates.S(0), gates.SX(1), gates.CNOT(0, 1), gates.X(0)]
)
standard_decompositions.add(gates.CCZ, [gates.H(2), gates.TOFFOLI(0, 1, 2), gates.H(2)])
standard_decompositions.add(
gates.TOFFOLI,
[
gates.H(2),
gates.CNOT(1, 2),
gates.TDG(2),
gates.CNOT(0, 2),
gates.T(2),
gates.CNOT(1, 2),
gates.T(1),
gates.TDG(2),
gates.CNOT(0, 2),
gates.CNOT(0, 1),
gates.T(2),
gates.T(0),
gates.TDG(1),
gates.H(2),
gates.CNOT(0, 1),
],
)
7 changes: 4 additions & 3 deletions tests/test_cirq.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import numpy as np
import pytest

from qibo import gates, models
from qibo import Circuit, gates, matrices
from qibo.backends import NumpyBackend
from qibo.models import QFT
from qibo.quantum_info import random_statevector, random_unitary

numpy_backend = NumpyBackend()
Expand Down Expand Up @@ -54,7 +55,7 @@ def assert_gates_equivalent(
if ndevices is not None:
accelerators = {"/GPU:0": ndevices}

c = models.Circuit(nqubits, accelerators)
c = Circuit(nqubits, accelerators)
c.add(qibo_gate)
assert c.depth == target_depth
if accelerators and not backend.supports_multigpu:
Expand Down Expand Up @@ -307,7 +308,7 @@ def test_unitary_matrix_gate_controlled_by(backend, nqubits, ntargets, ndevices)

@pytest.mark.parametrize("nqubits", [5, 6, 7, 11, 12])
def test_qft(backend, accelerators, nqubits):
c = models.QFT(nqubits, accelerators=accelerators)
c = QFT(nqubits, accelerators=accelerators)
initial_state = random_statevector(2**nqubits, backend=numpy_backend)
final_state = backend.execute_circuit(c, np.copy(initial_state)).state()
final_state = backend.cast(final_state, dtype=final_state.dtype)
Expand Down
8 changes: 8 additions & 0 deletions tests/test_gates_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from qibo import Circuit, gates, matrices
from qibo.parameter import Parameter
from qibo.quantum_info import random_hermitian, random_statevector, random_unitary
from qibo.transpiler.decompositions import standard_decompositions


def apply_gates(backend, gatelist, nqubits=None, initial_state=None):
Expand Down Expand Up @@ -1205,6 +1206,13 @@ def test_toffoli(backend, applyx):
assert not gates.TOFFOLI(0, 1, 2).clifford
assert gates.TOFFOLI(0, 1, 2).unitary

# test decomposition
decomposition = Circuit(3)
decomposition.add(standard_decompositions(gates.TOFFOLI(0, 1, 2)))
decomposition = decomposition.unitary(backend)

backend.assert_allclose(decomposition, backend.cast(matrices.TOFFOLI), atol=1e-10)


def test_ccz(backend):
nqubits = 3
Expand Down
Loading