Skip to content

Commit

Permalink
Add Open Graph tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wlcsm committed Aug 3, 2024
1 parent b2f05d4 commit 878173b
Show file tree
Hide file tree
Showing 20 changed files with 1,001 additions and 21 deletions.
34 changes: 31 additions & 3 deletions graphix/open_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,33 @@ class Measurement:
plane: str

def __eq__(self, other):
"""Compares if two measurements are equal
Example
-------
>>> from graphix.open_graph import Measurement
>>> Measurement(0.0, "XY") == Measurement(0.0, "XY")
True
>>> Measurement(0.0, "XY") == Measurement(0.0, "YZ")
False
>>> Measurement(0.1, "XY") == Measurement(0.0, "XY")
False
"""
return np.allclose(self.angle, other.angle) and self.plane == other.plane

def is_z_measurement(self) -> bool:
"""Indicates whether it is a Z measurement"""
"""Indicates whether it is a Z measurement
Example
-------
>>> from graphix.open_graph import Measurement
>>> Measurement(0.0, "XY").is_z_measurement()
True
>>> Measurement(0.0, "YZ").is_z_measurement()
False
>>> Measurement(0.1, "XY").is_z_measurement()
False
"""
return np.allclose(self.angle, 0.0) and self.plane == "XY"


Expand Down Expand Up @@ -376,14 +399,19 @@ def measurements(self) -> dict[int, Measurement]:
... }
True
"""
return {n[0]: n[1]["measurement"] for n in self.inside.nodes(data=True) if "measurement" in n[1]}
return {
n[0]: n[1]["measurement"]
for n in self.inside.nodes(data=True)
if "measurement" in n[1]
}

def perform_z_deletions_in_place(self):
"""Removes the Z-deleted nodes from the graph in place"""
z_measured_nodes = [
node
for node in self.inside.nodes
if "measurement" in self.inside.nodes[node] and self.inside.nodes[node]["measurement"].is_z_measurement()
if "measurement" in self.inside.nodes[node]
and self.inside.nodes[node]["measurement"].is_z_measurement()
]

for node in z_measured_nodes:
Expand Down
31 changes: 31 additions & 0 deletions tests/circuits/adder_n4.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
OPENQASM 2.0;
include "qelib1.inc";
qreg q[4];
creg c[4];
x q[0];
x q[1];
h q[3];
cx q[2],q[3];
t q[0];
t q[1];
t q[2];
tdg q[3];
cx q[0],q[1];
cx q[2],q[3];
cx q[3],q[0];
cx q[1],q[2];
cx q[0],q[1];
cx q[2],q[3];
tdg q[0];
tdg q[1];
tdg q[2];
t q[3];
cx q[0],q[1];
cx q[2],q[3];
s q[3];
cx q[3],q[0];
h q[3];
measure q[0] -> c[0];
measure q[1] -> c[1];
measure q[2] -> c[2];
measure q[3] -> c[3];
56 changes: 56 additions & 0 deletions tests/circuits/bell_n4.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Generated from Cirq v0.8.0

OPENQASM 2.0;
include "qelib1.inc";


// Qubits: [(0, 0), (0, 1), (1, 0), (1, 1)]
qreg q[4];
creg m_b[1];
creg m_y[1];
creg m_a[1];
creg m_x[1];


h q[0];
h q[1];
h q[3];
cx q[0],q[2];
rx(pi*-0.25) q[0];

// Gate: CNOT**0.5
ry(pi*-0.5) q[2];
u3(pi*0.5,0,pi*0.75) q[3];
u3(pi*0.5,0,pi*0.25) q[2];
rx(pi*0.5) q[3];
cx q[3],q[2];
rx(pi*0.25) q[3];
ry(pi*0.5) q[2];
cx q[2],q[3];
rx(pi*-0.5) q[2];
rz(pi*0.5) q[2];
cx q[3],q[2];
u3(pi*0.5,pi*0.5,pi*1.0) q[3];
u3(pi*0.5,pi*1.0,pi*1.0) q[2];
ry(pi*0.5) q[2];

// Gate: CNOT**0.5
ry(pi*-0.5) q[0];
u3(pi*0.5,0,pi*0.75) q[1];
u3(pi*0.5,0,pi*0.25) q[0];
rx(pi*0.5) q[1];
cx q[1],q[0];
rx(pi*0.25) q[1];
ry(pi*0.5) q[0];
cx q[0],q[1];
rx(pi*-0.5) q[0];
rz(pi*0.5) q[0];
cx q[1],q[0];
u3(pi*0.5,pi*0.5,pi*1.0) q[1];
u3(pi*0.5,pi*1.0,pi*1.0) q[0];
ry(pi*0.5) q[0];

measure q[2] -> m_b[0];
measure q[3] -> m_y[0];
measure q[0] -> m_a[0];
measure q[1] -> m_x[0];
14 changes: 14 additions & 0 deletions tests/circuits/deutsch_n2.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Implementation of Deutsch algorithm with two qubits for f(x)=x
OPENQASM 2.0;
include "qelib1.inc";

qreg q[2];
creg c[2];

x q[1];
h q[0];
h q[1];
cx q[0],q[1];
h q[0];
measure q[0] -> c[0];
measure q[1] -> c[1];
Loading

0 comments on commit 878173b

Please sign in to comment.