Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = 'graphix_ibmq'
copyright = '2023, Team Graphix'
author = 'Daichi Sasaki, Shinichi Sunami'
release = '0.0.1'
project = "graphix_ibmq"
copyright = "2023, Team Graphix"
author = "Daichi Sasaki, Shinichi Sunami"
release = "0.0.1"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
Expand Down Expand Up @@ -59,5 +59,5 @@ def setup(app):
"examples_dirs": "../../examples/gallery", # path to your example scripts
"gallery_dirs": "gallery", # path to where to save gallery generated output
#'expected_failing_examples': ['../../examples/ibm_device.py'],
'filename_pattern': '/'
"filename_pattern": "/",
}
53 changes: 31 additions & 22 deletions examples/gallery/aer_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@

First, let us import relevant modules and define additional gates and function we'll use:
"""
#%%
import numpy as np

# %%

import matplotlib.pyplot as plt
import networkx as nx
import random
import numpy as np
from graphix import Circuit
from graphix_ibmq.runner import IBMQBackend
from qiskit.tools.visualization import plot_histogram
from qiskit_aer.noise import NoiseModel, depolarizing_error

from graphix_ibmq.runner import IBMQBackend


def cp(circuit, theta, control, target):
"""Controlled phase gate, decomposed"""
Expand All @@ -34,19 +36,21 @@ def swap(circuit, a, b):
circuit.cnot(a, b)


#%%
rng = np.random.default_rng(seed=100)


# %%
# Now let us define a circuit to apply QFT to three-qubit state and transpile into MBQC measurement pattern using graphix.

circuit = Circuit(3)
for i in range(3):
circuit.h(i)

psi = {}
random.seed(100)
# prepare random state for each input qubit
for i in range(3):
theta = random.uniform(0, np.pi)
phi = random.uniform(0, 2 * np.pi)
theta = rng.uniform(0, np.pi)
phi = rng.uniform(0, 2 * np.pi)
circuit.ry(i, theta)
circuit.rz(i, phi)
psi[i] = [np.cos(theta / 2), np.sin(theta / 2) * np.exp(1j * phi)]
Expand All @@ -72,11 +76,10 @@ def swap(circuit, a, b):
g = nx.Graph()
g.add_nodes_from(nodes)
g.add_edges_from(edges)
np.random.seed(100)
nx.draw(g)
plt.show()

#%%
# %%
# Now let us convert the pattern to qiskit circuit.

# minimize the space to save memory during aer simulation
Expand All @@ -87,13 +90,13 @@ def swap(circuit, a, b):
backend = IBMQBackend(pattern)
print(type(backend.circ))

#%%
# %%
# We can now simulate the circuit with Aer.

# run and get counts
result = backend.simulate()

#%%
# %%
# We can also simulate the circuit with noise model

# create an empty noise model
Expand All @@ -105,17 +108,17 @@ def swap(circuit, a, b):
# print noise model info
print(noise_model)

#%%
# %%
# Now we can run the simulation with noise model

# run and get counts
result_noise = backend.simulate(noise_model=noise_model)


#%%
# %%
# Now let us compare the results

# calculate the analytical
# calculate the analytical
state = [0] * 8
omega = np.exp(1j * np.pi / 4)

Expand All @@ -129,17 +132,23 @@ def swap(circuit, a, b):
count_theory[f"{i:03b}"] = 1024 * np.abs(state[i]) ** 2

# plot and compare the results
fig, ax = plt.subplots(figsize=(7,5))
plot_histogram([count_theory, result, result_noise],
legend=["theoretical probability", "execution result", "Aer simulation w/ noise model"],
ax=ax,
bar_labels=False)
fig, ax = plt.subplots(figsize=(7, 5))
plot_histogram(
[count_theory, result, result_noise],
legend=[
"theoretical probability",
"execution result",
"Aer simulation w/ noise model",
],
ax=ax,
bar_labels=False,
)
legend = ax.legend(fontsize=18)
legend = ax.legend(loc='upper left')
legend = ax.legend(loc="upper left")
# %%


#%%
# %%
# Example demonstrating how to run a pattern on an IBM Quantum device. All explanations are provided as comments.

# First, load the IBMQ account using an API token.
Expand Down
3 changes: 1 addition & 2 deletions examples/gallery/qiskit_to_graphix.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
First, let us import relevant modules and define quantum circuit we want to convert:
"""


# %%
from qiskit import QuantumCircuit, transpile
from qiskit import transpile
from qiskit.circuit.random.utils import random_circuit

qc = random_circuit(5, 2, seed=42)
Expand Down
57 changes: 33 additions & 24 deletions examples/ibm_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@

First, let us import relevant modules and define additional gates and function we'll use:
"""
#%%
import numpy as np

# %%

import os

import matplotlib.pyplot as plt
import networkx as nx
import random
import numpy as np
from graphix import Circuit
from graphix_ibmq.runner import IBMQBackend
from qiskit_ibm_provider import IBMProvider
from qiskit.tools.visualization import plot_histogram
from qiskit.providers.fake_provider import FakeLagos
from qiskit.tools.visualization import plot_histogram
from qiskit_ibm_provider import IBMProvider

from graphix_ibmq.runner import IBMQBackend


def cp(circuit, theta, control, target):
Expand All @@ -36,7 +39,10 @@ def swap(circuit, a, b):
circuit.cnot(a, b)


#%%
rng = np.random.default_rng(seed=100)
token = os.environ["API_TOKEN"]

# %%
# Now let us define a circuit to apply QFT to three-qubit state.

circuit = Circuit(3)
Expand All @@ -46,8 +52,8 @@ def swap(circuit, a, b):
psi = {}
# prepare random state for each input qubit
for i in range(3):
theta = random.uniform(0, np.pi)
phi = random.uniform(0, 2 * np.pi)
theta = rng.uniform(0, np.pi)
phi = rng.uniform(0, 2 * np.pi)
circuit.ry(i, theta)
circuit.rz(i, phi)
psi[i] = [np.cos(theta / 2), np.sin(theta / 2) * np.exp(1j * phi)]
Expand All @@ -73,11 +79,10 @@ def swap(circuit, a, b):
g = nx.Graph()
g.add_nodes_from(nodes)
g.add_edges_from(edges)
np.random.seed(100)
nx.draw(g)
plt.show()

#%%
# %%
# Now let us convert the pattern to qiskit circuit.

# minimize the space to save memory during aer simulation.
Expand All @@ -88,34 +93,34 @@ def swap(circuit, a, b):
backend.to_qiskit()
print(type(backend.circ))

#%%
# %%
# load the account with API token
IBMProvider.save_account(token='MY API TOKEN')
IBMProvider.save_account(token=token)

# get the device backend
instance_name = 'ibm-q/open/main'
instance_name = "ibm-q/open/main"
backend_name = "ibm_lagos"
backend.get_backend(instance=instance_name,resource=backend_name)
backend.get_backend(instance=instance_name, resource=backend_name)

#%%
# %%
# Get provider and the backend.

instance_name = "ibm-q/open/main"
backend_name = "ibm_lagos"

backend.get_backend(instance=instance_name, resource=backend_name)

#%%
# %%
# We can now execute the circuit on the device backend.

result = backend.run()

#%%
# %%
# Retrieve the job if needed

# result = backend.retrieve_result("Job ID")

#%%
# %%
# We can simulate the circuit with noise model based on the device we used

# get the noise model of the device backend
Expand All @@ -124,7 +129,7 @@ def swap(circuit, a, b):
# execute noisy simulation and get counts
result_noise = backend.simulate(noise_model=backend_noisemodel)

#%%
# %%
# Now let us compare the results with theoretical output

# calculate the theoretical output state
Expand All @@ -141,12 +146,16 @@ def swap(circuit, a, b):
count_theory[f"{i:03b}"] = 1024 * np.abs(state[i]) ** 2

# plot and compare the results
fig, ax = plt.subplots(figsize=(7,5))
fig, ax = plt.subplots(figsize=(7, 5))
plot_histogram(
[count_theory, result, result_noise],
legend=["theoretical probability", "execution result", "Aer simulation w/ noise model"],
legend=[
"theoretical probability",
"execution result",
"Aer simulation w/ noise model",
],
ax=ax,
bar_labels=False
bar_labels=False,
)
legend = ax.legend(fontsize=18)
legend = ax.legend(loc='upper left')
legend = ax.legend(loc="upper left")
1 change: 1 addition & 0 deletions graphix_ibmq/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Graphix IBMQ interface."""
64 changes: 0 additions & 64 deletions graphix_ibmq/clifford.py

This file was deleted.

3 changes: 3 additions & 0 deletions graphix_ibmq/converter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""Qiskit to graphix circuit converter."""

from graphix import Circuit
from qiskit import QuantumCircuit, transpile

Expand All @@ -14,6 +16,7 @@ def qiskit_to_graphix(qc: QuantumCircuit) -> Circuit:

Returns:
Circuit: Converted graphix circuit

"""
if not isinstance(qc, QuantumCircuit):
raise TypeError(f"qc must be QuantumCircuit, not {type(qc)}")
Expand Down
Loading
Loading