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

Add gates.gate.CCZ #1280

Merged
merged 12 commits into from
Jun 3, 2024
7 changes: 7 additions & 0 deletions doc/source/api-reference/qibo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,13 @@ Toffoli
:members:
:member-order: bysource

CCZ
"""

.. autoclass:: qibo.gates.CCZ
:members:
:member-order: bysource

Deutsch
"""""""

Expand Down
1 change: 1 addition & 0 deletions src/qibo/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def create(self, dtype):
self.ECR = self.matrices.ECR
self.SYC = self.matrices.SYC
self.TOFFOLI = self.matrices.TOFFOLI
self.CCZ = self.matrices.CCZ


matrices = QiboMatrices()
Expand Down
16 changes: 16 additions & 0 deletions src/qibo/backends/npmatrices.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,22 @@ def TOFFOLI(self):
dtype=self.dtype,
)

@cached_property
def CCZ(self):
return self._cast(
[
[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, -1],
],
dtype=self.dtype,
)

def DEUTSCH(self, theta):
sin = self.np.sin(theta) + 0j # 0j necessary for right tensorflow dtype
cos = self.np.cos(theta) + 0j
Expand Down
50 changes: 50 additions & 0 deletions src/qibo/gates/gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -2308,6 +2308,56 @@ def congruent(self, use_toffolis: bool = True) -> List[Gate]:
]


class CCZ(Gate):
"""The controlled-CZ gate.

Corresponds to the following unitary matrix

.. math::
\\begin{pmatrix}
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\\\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\\\
0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\\\
0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 \\\\
0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 \\\\
0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 \\\\
0 & 0 & 0 & 0 & 0 & 0 & 1 & 0 \\\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & -1 \\\\
\\end{pmatrix}

Args:
q0 (int): the first control qubit id number.
q1 (int): the second control qubit id number.
q2 (int): the target qubit id number.
"""

def __init__(self, q0, q1, q2):
super().__init__()
self.name = "ccz"
self.draw_label = "Z"
self.control_qubits = (q0, q1)
self.target_qubits = (q2,)
self.init_args = [q0, q1, q2]
self.unitary = True

@property
def qasm_label(self):
return "ccz"

def decompose(self) -> List[Gate]:
"""Decomposition of :math:`\\text{CCZ}` gate.

Decompose :math:`\\text{CCZ}` gate into :class:`qibo.gates.H` in
the target qubit, followed by :class:`qibo.gates.TOFFOLI`, followed
by a :class:`qibo.gates.H` in the target qubit.
"""
from qibo.transpiler.decompositions import ( # pylint: disable=C0415
standard_decompositions,
)

return standard_decompositions(self)


class DEUTSCH(ParametrizedGate):
"""The Deutsch gate.

Expand Down
1 change: 1 addition & 0 deletions src/qibo/transpiler/decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,4 @@ def _u3_to_gpi2(t, p, l):
standard_decompositions.add(
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)])
42 changes: 41 additions & 1 deletion tests/test_gates_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np
import pytest

from qibo import gates
from qibo import Circuit, gates, matrices
from qibo.parameter import Parameter
from qibo.quantum_info import random_hermitian, random_statevector, random_unitary

Expand Down Expand Up @@ -1206,6 +1206,46 @@ def test_toffoli(backend, applyx):
assert gates.TOFFOLI(0, 1, 2).unitary


def test_ccz(backend):
nqubits = 3
initial_state = random_statevector(2**nqubits, backend=backend)
final_state = apply_gates(
backend,
[gates.CCZ(0, 1, 2)],
nqubits=nqubits,
initial_state=initial_state,
)

matrix = np.array(
[
[1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, -1],
],
dtype=np.complex128,
)
matrix = backend.cast(matrix, dtype=matrix.dtype)

target_state = matrix @ initial_state
backend.assert_allclose(final_state, target_state)

assert gates.CCZ(0, 1, 2).qasm_label == "ccz"
assert not gates.CCZ(0, 1, 2).clifford
assert gates.CCZ(0, 1, 2).unitary

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

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


def test_deutsch(backend):
theta = 0.1234
nqubits = 3
Expand Down
Loading