From 14a86f6694ba94c79eca9c98917e1048d5925650 Mon Sep 17 00:00:00 2001 From: Roland Siegbert Date: Sun, 21 Apr 2024 21:20:21 +0200 Subject: [PATCH] Add additional circuit example for direct OpenQASM import --- .../includes/circuits/circuits1.qasm | 10 +++++ .../tutorials/includes/circuits/circuits2.py | 45 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 doc/source/tutorials/includes/circuits/circuits1.qasm create mode 100644 doc/source/tutorials/includes/circuits/circuits2.py diff --git a/doc/source/tutorials/includes/circuits/circuits1.qasm b/doc/source/tutorials/includes/circuits/circuits1.qasm new file mode 100644 index 000000000..8f0c637e2 --- /dev/null +++ b/doc/source/tutorials/includes/circuits/circuits1.qasm @@ -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]; diff --git a/doc/source/tutorials/includes/circuits/circuits2.py b/doc/source/tutorials/includes/circuits/circuits2.py new file mode 100644 index 000000000..68e7600c8 --- /dev/null +++ b/doc/source/tutorials/includes/circuits/circuits2.py @@ -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()