Tutorial
In this tutorial, we will learn how to use the graphix-perceval library to convert graphix.Pattern objects into perceval.Circuit objects.
Installation
First, we need to install the graphix-perceval library. This can be done using pip:
pip install graphix-perceval
If you have not installed graphix yet, you can install it using pip as well:
pip install graphix
Generating MBQC Pattern
We first generate a MBQC pattern using graphix library.
We create GHZ state as an example.
First, let us import relevant modules and define function we will use:
from graphix import Circuit
import numpy as np
import matplotlib.pyplot as plt
import networkx as nx
# define the functions required for GHZ state generation
def ghz(circuit: Circuit):
"""generate GHZ circuit"""
circuit.h(1)
circuit.h(2)
circuit.cnot(0, 1)
circuit.cnot(0, 2)
Then we define a circuit to create GHZ state.
# generate the GHZ state generation pattern
circuit = Circuit(3)
ghz(circuit)
pattern = circuit.transpile()
# plot the pattern
nodes, edges = pattern.get_graph()
g = nx.Graph()
g.add_nodes_from(nodes)
g.add_edges_from(edges)
np.random.seed(100)
nx.draw(g)
plt.show()
Pattern-to-circuit conversion
Now let us convert the pattern into a circuit using the graphix-perceval library:
from graphix_perceval import to_perceval
from perceval import pdisplay
exp = to_perceval(pattern)
pdisplay(exp.circ)
Running pattern on Perceval simulator
By running the Perceval’s computing backends, We can obtain the probability distribution of the measurement outcomes
exp.set_local_processor("SLOS")
dist = exp.get_probability_distribution()
dist.draw()
| state | probability |
|---|---|
| |000> | 0.5 |
| |111> | 0.5 |
or sampling distribution with a given number of samples:
exp.set_local_processor("SLOS")
dist = exp.sample(num_samples=1000)
dist.draw()
| state | counts |
|---|---|
| |000> | 499 |
| |111> | 501 |
Note
Note that the current implementation only supports SLOS and Naive as local Perceval processors.
See Perceval documentation for more details.