-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add additional circuit example for direct OpenQASM import
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |