From 10579f28789959bc73ee471170c4f1b3e353563a Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 21 Mar 2024 09:23:04 +0400 Subject: [PATCH 1/8] gate implementation --- src/qibo/backends/npmatrices.py | 16 +++++++++ src/qibo/gates/gates.py | 50 +++++++++++++++++++++++++++ src/qibo/transpiler/decompositions.py | 1 + 3 files changed, 67 insertions(+) diff --git a/src/qibo/backends/npmatrices.py b/src/qibo/backends/npmatrices.py index c6e447e9ea..0f0d5fce27 100644 --- a/src/qibo/backends/npmatrices.py +++ b/src/qibo/backends/npmatrices.py @@ -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 diff --git a/src/qibo/gates/gates.py b/src/qibo/gates/gates.py index dfbe628a00..b19ad5acb4 100644 --- a/src/qibo/gates/gates.py +++ b/src/qibo/gates/gates.py @@ -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. diff --git a/src/qibo/transpiler/decompositions.py b/src/qibo/transpiler/decompositions.py index f559f0088a..33ab35b56b 100644 --- a/src/qibo/transpiler/decompositions.py +++ b/src/qibo/transpiler/decompositions.py @@ -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)]) From bb513e079608f678a02bd2bac9d1ddf5615177cc Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 21 Mar 2024 09:23:59 +0400 Subject: [PATCH 2/8] api ref --- doc/source/api-reference/qibo.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/doc/source/api-reference/qibo.rst b/doc/source/api-reference/qibo.rst index c574c90145..c39b9d0f41 100644 --- a/doc/source/api-reference/qibo.rst +++ b/doc/source/api-reference/qibo.rst @@ -923,6 +923,13 @@ Toffoli :members: :member-order: bysource +CCZ +""" + +.. autoclass:: qibo.gates.CCZ + :members: + :member-order: bysource + Deutsch """"""" From aa290ed2ea7adcbd1b3f17b9453986873444e513 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 21 Mar 2024 09:29:10 +0400 Subject: [PATCH 3/8] test --- tests/test_gates_gates.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/test_gates_gates.py b/tests/test_gates_gates.py index d407e72e9e..759cd3961b 100644 --- a/tests/test_gates_gates.py +++ b/tests/test_gates_gates.py @@ -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 From 78e9fe459349c4fad6458876017cb2f2701327cf Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 21 Mar 2024 10:37:41 +0400 Subject: [PATCH 4/8] `qibojit` branch --- pyproject.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2febb035ff..b8a63978a4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" @@ -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] @@ -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] From a4e53d483e2190b4e391d658559142971991630d Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Thu, 21 Mar 2024 10:42:21 +0400 Subject: [PATCH 5/8] update lock --- poetry.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/poetry.lock b/poetry.lock index 34730c958e..e28e5bf29e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -4133,8 +4133,8 @@ scipy = "^1.10.1" [package.source] type = "git" url = "https://github.com/qiboteam/qibojit.git" -reference = "HEAD" -resolved_reference = "5af642977fd66a4268608fbe2986e55160a88fe4" +reference = "ccz" +resolved_reference = "82a0bf0976a44ec54d1d46b82b716aabdf9c5d25" [[package]] name = "qibotn" @@ -5557,4 +5557,4 @@ torch = ["torch"] [metadata] lock-version = "2.0" python-versions = ">=3.9,<3.12" -content-hash = "b167edb192763e8c70eeef729e281f4ebbef8bdee41eb4d8692e3784babfe75e" +content-hash = "55c0840ff1101c940cc076d63aec414b437415dc62a067ac169181ad1438ca70" From 24b0f08d3ac9e4171fccb0a8ef1c707822223f73 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Fri, 22 Mar 2024 08:35:07 +0400 Subject: [PATCH 6/8] fix coverage --- src/qibo/backends/__init__.py | 1 + tests/test_gates_gates.py | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/qibo/backends/__init__.py b/src/qibo/backends/__init__.py index a80c9932e1..cca6f9f010 100644 --- a/src/qibo/backends/__init__.py +++ b/src/qibo/backends/__init__.py @@ -145,6 +145,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() diff --git a/tests/test_gates_gates.py b/tests/test_gates_gates.py index 759cd3961b..393a3accf5 100644 --- a/tests/test_gates_gates.py +++ b/tests/test_gates_gates.py @@ -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 @@ -1238,6 +1238,13 @@ def test_ccz(backend): 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 From 9ac51ae94975f191e5fe5a3a99fa566709c015f5 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Sat, 23 Mar 2024 08:45:13 +0400 Subject: [PATCH 7/8] remove qibojit branch --- pyproject.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index b8a63978a4..2febb035ff 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" , branch = "ccz" } +qibojit = { git = "https://github.com/qiboteam/qibojit.git" } qibotn = { git = "https://github.com/qiboteam/qibotn.git" } stim = "^1.12.0" @@ -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" , branch = "ccz" } +qibojit = { git = "https://github.com/qiboteam/qibojit.git" } qibotn = { git = "https://github.com/qiboteam/qibotn.git" } [tool.poetry.group.cuda12] @@ -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" , branch = "ccz" } +qibojit = { git = "https://github.com/qiboteam/qibojit.git" } qibotn = { git = "https://github.com/qiboteam/qibotn.git" } [tool.poetry.extras] From e563d7f78a235b26a93d9a3f210284bb234b2bd8 Mon Sep 17 00:00:00 2001 From: Renato Mello Date: Sat, 23 Mar 2024 08:48:30 +0400 Subject: [PATCH 8/8] update lock --- poetry.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/poetry.lock b/poetry.lock index e28e5bf29e..34730c958e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -4133,8 +4133,8 @@ scipy = "^1.10.1" [package.source] type = "git" url = "https://github.com/qiboteam/qibojit.git" -reference = "ccz" -resolved_reference = "82a0bf0976a44ec54d1d46b82b716aabdf9c5d25" +reference = "HEAD" +resolved_reference = "5af642977fd66a4268608fbe2986e55160a88fe4" [[package]] name = "qibotn" @@ -5557,4 +5557,4 @@ torch = ["torch"] [metadata] lock-version = "2.0" python-versions = ">=3.9,<3.12" -content-hash = "55c0840ff1101c940cc076d63aec414b437415dc62a067ac169181ad1438ca70" +content-hash = "b167edb192763e8c70eeef729e281f4ebbef8bdee41eb4d8692e3784babfe75e"