Note
Go to the end to download the full example code.
From Circuit to Executable Pattern¶
This example transpiles an MBQC-native circuit into GraphQOMB’s compiler IRs, then lowers them to an executable pattern and validates it by simulation.
import numpy as np
from graphqomb.circuit import MBQCCircuit, circuit2graph
from graphqomb.pattern import print_pattern
from graphqomb.qompiler import qompile
from graphqomb.simulator import (
CircuitSimulator,
PatternSimulator,
SimulatorBackend,
)
generate circuit
circuit = MBQCCircuit(3)
circuit.j(0, 0.5 * np.pi)
circuit.cz(0, 1)
circuit.cz(0, 2)
circuit.j(1, 0.75 * np.pi)
circuit.j(2, 0.25 * np.pi)
circuit.cz(0, 2)
circuit.cz(1, 2)
Convert the circuit into GraphQOMB’s compiler IRs.
graphstate, xflow, scheduler = circuit2graph(circuit)
print("graph nodes:", len(graphstate.physical_nodes))
print("graph edges:", len(graphstate.physical_edges))
print("feedforward entries:", len(xflow))
print("scheduled slices:", scheduler.num_slices())
# Lower the IRs into an executable pattern using the derived schedule.
pattern = qompile(graphstate, xflow, scheduler=scheduler)
print("pattern depth:", pattern.depth)
print("pattern max space:", pattern.max_space)
print("pattern active volume:", pattern.active_volume)
print_pattern(pattern)
graph nodes: 6
graph edges: 7
feedforward entries: 3
scheduled slices: 3
pattern depth: 3
pattern max space: 5
pattern active volume: 15
N: node=3
E: nodes=(2, 3)
E: nodes=(0, 3)
E: nodes=(1, 3)
TICK
N: node=4
N: node=5
E: nodes=(1, 4)
E: nodes=(4, 5)
E: nodes=(2, 5)
E: nodes=(3, 5)
M: node=0, plane=Plane.XY, angle=-1.5707963267948966
TICK
M: node=1, plane=Plane.XY, angle=-2.356194490192345
M: node=2, plane=Plane.XY, angle=-0.7853981633974483
TICK
X: node=3
Z: node=3
X: node=4
Z: node=4
X: node=5
Z: node=5
0 more commands truncated. Change lim argument of print_pattern() to show more
simulate the pattern
simulator = PatternSimulator(pattern, SimulatorBackend.StateVector)
simulator.simulate()
state = simulator.state
statevec = state.state()
# check by comparing the circuit simulator
circ_simulator = CircuitSimulator(circuit, SimulatorBackend.StateVector)
circ_simulator.simulate()
circ_state = circ_simulator.state.state()
inner_product = np.vdot(statevec, circ_state)
print("inner product:", np.abs(inner_product)) # should be 1
inner product: 0.9999999999999993
Total running time of the script: (0 minutes 0.004 seconds)