Skip to content

Commit

Permalink
add and test cz gate
Browse files Browse the repository at this point in the history
  • Loading branch information
dakk committed Oct 18, 2024
1 parent 3dc88c1 commit ddc796d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion qlasskit/ast2ast/astrewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions qlasskit/qcircuit/gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions qlasskit/qcircuit/qcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]
Expand Down
21 changes: 20 additions & 1 deletion test/test_qcircuit_exporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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"),
(
Expand Down Expand Up @@ -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}),
],
Expand All @@ -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(),
{
Expand Down Expand Up @@ -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),
Expand All @@ -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]),
Expand All @@ -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])

0 comments on commit ddc796d

Please sign in to comment.