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
Merged
Show file tree
Hide file tree
Changes from 5 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
7 changes: 7 additions & 0 deletions doc/source/api-reference/qibo.rst
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,13 @@ Toffoli
:members:
:member-order: bysource

CCZ
"""

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

Deutsch
"""""""

Expand Down
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pylint = "^3.0.3"
matplotlib = "^3.7.0"
tensorflow = { version = "^2.14.1,<2.16", markers = "sys_platform == 'linux'" }
torch = "^2.1.1"
qibojit = { git = "https://github.com/qiboteam/qibojit.git" }
qibojit = { git = "https://github.com/qiboteam/qibojit.git" , branch = "ccz" }
qibotn = { git = "https://github.com/qiboteam/qibotn.git" }
stim = "^1.12.0"

Expand All @@ -87,7 +87,7 @@ optional = true
[tool.poetry.group.cuda11.dependencies]
cupy-cuda11x = "^12.0.0"
cuquantum-python-cu11 = "^23.3.0"
qibojit = { git = "https://github.com/qiboteam/qibojit.git" }
qibojit = { git = "https://github.com/qiboteam/qibojit.git" , branch = "ccz" }
qibotn = { git = "https://github.com/qiboteam/qibotn.git" }

[tool.poetry.group.cuda12]
Expand All @@ -96,7 +96,7 @@ optional = true
[tool.poetry.group.cuda12.dependencies]
cupy-cuda12x = "^12.0.0"
cuquantum-python-cu12 = "^23.3.0"
qibojit = { git = "https://github.com/qiboteam/qibojit.git" }
qibojit = { git = "https://github.com/qiboteam/qibojit.git" , branch = "ccz" }
qibotn = { git = "https://github.com/qiboteam/qibotn.git" }

[tool.poetry.extras]
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 @@ -2307,6 +2307,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)])
33 changes: 33 additions & 0 deletions tests/test_gates_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,39 @@ 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


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