Skip to content

Commit

Permalink
Add additional circuit example for direct OpenQASM import
Browse files Browse the repository at this point in the history
  • Loading branch information
rscircus committed Jul 2, 2024
1 parent f94276b commit 14a86f6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
10 changes: 10 additions & 0 deletions doc/source/tutorials/includes/circuits/circuits1.qasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Generated by QIBO 0.2.4
OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
creg a[2];
cz q[0],q[2];
gpi2(0.3) q[1];
cz q[1],q[2];
measure q[0] -> a[0];
measure q[2] -> a[1];
45 changes: 45 additions & 0 deletions doc/source/tutorials/includes/circuits/circuits2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import matplotlib.pyplot as plt
import numpy as np

from qibolab import execute_qasm

# define the circuit in OpenQASM
circuit = """// Generated by QIBO 0.2.4
OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
creg a[2];
cz q[0],q[2];
gpi2(0.3) q[1];
cz q[1],q[2];
measure q[0] -> a[0];
measure q[2] -> a[1];"""

## or read in an assembly-file

# set load_circuit to 'True' to overwrite the circuit above
load_circuit = False
if load_circuit:
try:
with open("./circuits1.qasm" "r") as qasm_file:
circuit = qasm_file.read()
except FileNotFoundError:
print("File not found!")
except Exception as e:
print(f"An error occurred: {e}")

measurement = execute_qasm(circuit, platform="dummy", nshots=4000)

freq = measurement.frequencies()
p0 = freq["0"] / 4000 if "0" in freq else 0
p1 = freq["1"] / 4000 if "1" in freq else 0
hardware = [p0, p1]

# plot results
exp_angles = np.arange(0, 2 * np.pi, np.pi / 16)
plt.plot(exp_angles, hardware, label="qibolab hardware")

plt.legend()
plt.ylabel("P(1)")
plt.xlabel("Rotation [rad]")
plt.show()

0 comments on commit 14a86f6

Please sign in to comment.