From ddc796d2d28efcaff00d31445cb7c6f8369ff3d9 Mon Sep 17 00:00:00 2001 From: "Davide Gessa (dakk)" Date: Fri, 18 Oct 2024 09:10:40 +0200 Subject: [PATCH] add and test cz gate --- qlasskit/ast2ast/astrewriter.py | 2 +- qlasskit/qcircuit/gates.py | 5 +++++ qlasskit/qcircuit/qcircuit.py | 6 ++++++ test/test_qcircuit_exporters.py | 21 ++++++++++++++++++++- 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/qlasskit/ast2ast/astrewriter.py b/qlasskit/ast2ast/astrewriter.py index cd0de70..3a376d8 100644 --- a/qlasskit/ast2ast/astrewriter.py +++ b/qlasskit/ast2ast/astrewriter.py @@ -195,7 +195,7 @@ def visit_Subscript(self, node): # noqa: C901 elts = tup.elts ifex = elts[0] - + for i, x in enumerate(elts[1:]): ifex = ast.IfExp( test=ast.Compare( diff --git a/qlasskit/qcircuit/gates.py b/qlasskit/qcircuit/gates.py index 3db633e..3f67138 100644 --- a/qlasskit/qcircuit/gates.py +++ b/qlasskit/qcircuit/gates.py @@ -105,6 +105,11 @@ def __init__(self): super().__init__(X(), 1) +class CZ(QControlledGate): + def __init__(self): + super().__init__(Z(), 1) + + class CP(QControlledGate): def __init__(self): super().__init__(P(), 1) diff --git a/qlasskit/qcircuit/qcircuit.py b/qlasskit/qcircuit/qcircuit.py index 9ec788f..75f9e36 100644 --- a/qlasskit/qcircuit/qcircuit.py +++ b/qlasskit/qcircuit/qcircuit.py @@ -73,6 +73,7 @@ def random(qubits_n: int, depth: int, gate_list=None) -> "QCircuit": gates.X, gates.Z, gates.Y, + gates.CZ, gates.CX, gates.CCX, gates.H, @@ -287,6 +288,11 @@ def ccx(self, w1, w2, w3): w1, w2, w3 = self[w1], self[w2], self[w3] self.append(gates.CCX(), [w1, w2, w3]) + def cz(self, w1, w2): + """CZ gate""" + w1, w2 = self[w1], self[w2] + self.append(gates.CZ(), [w1, w2]) + def mctrl(self, g, wl: List[int], target, param=None): """Multi controlled gate""" target = self[target] diff --git a/test/test_qcircuit_exporters.py b/test/test_qcircuit_exporters.py index ecd57e0..bbaa1d7 100644 --- a/test/test_qcircuit_exporters.py +++ b/test/test_qcircuit_exporters.py @@ -34,6 +34,14 @@ def cx_circuit(): return qc +def cz_circuit(): + qc = QCircuit() + a, b = qc.add_qubit(), qc.add_qubit() + qc.x(a) + qc.cz(a, b) + return qc + + def ccx_circuit(): qc = QCircuit() a, b, c = qc.add_qubit("a"), qc.add_qubit("b"), qc.add_qubit("c") @@ -63,6 +71,7 @@ def qft_circuit(): ("qc", "result"), [ (cx_circuit(), "gate qc q0 q1 {\n\tx q0\n\tcx q0 q1\n}\n\n"), + (cz_circuit(), "gate qc q0 q1 {\n\tx q0\n\tcz q0 q1\n}\n\n"), (ccx_circuit(), "gate qc a b c {\n\tx a\n\tx b\n\tccx a b c\n}\n\n"), (bell_circuit(), "gate qc a b {\n\th a\n\tcx a b\n}\n\n"), ( @@ -115,6 +124,7 @@ def test_export_sympy_gate(self): ("qc", "result"), [ (cx_circuit(), {"11": 1}), + (cz_circuit(), {"01": 1}), (ccx_circuit(), {"111": 1}), (qft_circuit(), {"000": 1}), ], @@ -136,6 +146,13 @@ def test_export_qiskit(self): "q(1)": np.array([[[1]]], dtype=np.int8), }, ), + ( + cz_circuit(), + { + "q(0)": np.array([[[1]]], dtype=np.int8), + "q(1)": np.array([[[0]]], dtype=np.int8), + }, + ), ( ccx_circuit(), { @@ -186,6 +203,7 @@ def test_export_cirq_gate(self): ("qc", "result", "wires"), [ (cx_circuit(), [0, 0, 0, 1], 2), + (cz_circuit(), [0, 0, 1, 0], 2), (ccx_circuit(), [0, 0, 0, 0, 0, 0, 0, 1], 3), (bell_circuit(), [0.5, 0, 0, 0.5], 2), (qft_circuit(), [1, 0, 0, 0, 0, 0, 0, 0], 3), @@ -209,6 +227,7 @@ def test_export_pennylane_circuit(self): ("qc", "result"), [ (cx_circuit(), [0, 0, 0, 1]), + (cz_circuit(), [0, 0, 1, 0]), (ccx_circuit(), [0, 0, 0, 0, 0, 0, 0, 1]), (bell_circuit(), [0.70710678, 0, 0, 0.70710678]), (qft_circuit(), [1, 0, 0, 0, 0, 0, 0, 0]), @@ -225,5 +244,5 @@ def test_export_qutip_circuit(self): self.assertEqual(probabilities, [1]) - for i in zip(states[0].data.to_array(), self.result): + for i in zip(states[0].data.toarray(), self.result): self.assertAlmostEqual(float(i[0][0]), i[1])