Skip to content

Commit 62f13fa

Browse files
mdepasca-mqvMShahzaib
andauthored
🚸 Add examples (#22)
* 🚸 Add examples * πŸ› Imports all on top * πŸ› dummy commit * πŸ› Removed unused imported module * πŸ”¨ Add matplotlib as dev dependency for example tests * πŸ‘· updated CI.yml to install dev dependencies * ✨ Add example scripts using dotenv * 🚧 add example scripts * 🚧Address PR comments and update examples --------- Co-authored-by: MShahzaib <shahzaib@lrz.de>
1 parent 9a3fdb1 commit 62f13fa

5 files changed

Lines changed: 178 additions & 1 deletion

File tree

β€Ž.github/workflows/ci.ymlβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
python -m pip install --upgrade pip
3131
pip install nox
3232
pip install uv
33-
uv sync
33+
uv sync --group dev
3434
- name: Run pre-commit
3535
run: |
3636
uv run pre-commit install

β€Žexamples/GHZ_measure.pyβ€Ž

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# GHZ State generation and measurement
2+
3+
import os
4+
5+
import matplotlib.pyplot as plt
6+
from dotenv import load_dotenv
7+
from mqss.qiskit_adapter import MQSSQiskitAdapter
8+
from qiskit import QuantumCircuit, transpile
9+
from qiskit.visualization import plot_histogram
10+
11+
# Load environment variables
12+
load_dotenv()
13+
token = os.getenv("MQP_TOKEN")
14+
backend_name = os.getenv("MQP_BACKEND")
15+
16+
adapter = MQSSQiskitAdapter(token=token)
17+
backend = adapter.get_backend(backend_name)
18+
19+
# Parameters
20+
qubits = 8
21+
shots = 200
22+
23+
# Build GHZ circuit
24+
qc = QuantumCircuit(qubits, qubits)
25+
qc.h(0)
26+
for i in range(1, qubits):
27+
qc.cx(0, i)
28+
qc.measure_all(add_bits=False)
29+
30+
# Transpile if needed
31+
trans_qc = transpile(qc, backend, optimization_level=3)
32+
33+
# Run job
34+
job = backend.run(trans_qc, no_modify=True, shots=shots, queued=True)
35+
counts = job.result().get_counts()
36+
print("Result:", counts)
37+
38+
# Plot
39+
plot_histogram(counts, figsize=(12, 6))
40+
plt.show()
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Inverse Unitary Test
2+
# Demonstrates how reversible a circuit is on simulator vs real hardware.
3+
4+
import os
5+
6+
import matplotlib.pyplot as plt
7+
import numpy as np
8+
from dotenv import load_dotenv
9+
from mqss.qiskit_adapter import MQSSQiskitAdapter
10+
from qiskit import QuantumCircuit
11+
from qiskit.visualization import plot_histogram
12+
13+
# Load environment variables
14+
load_dotenv()
15+
token = os.getenv("MQP_TOKEN")
16+
backend_name = os.getenv("MQP_BACKEND")
17+
18+
adapter = MQSSQiskitAdapter(token=token)
19+
backend = adapter.get_backend(backend_name)
20+
21+
n = 3
22+
x = 3 # |011⟩
23+
24+
qc = QuantumCircuit(n)
25+
26+
# Prepare |x⟩
27+
for i in range(n):
28+
if (x >> i) & 1:
29+
qc.x(i)
30+
31+
# Unitaries
32+
for qubit in range(n):
33+
qc.h(qubit)
34+
for target in range(qubit + 1, n):
35+
qc.crz(np.pi / 2 ** (target - qubit), qubit, target)
36+
37+
# Inverse unitaries
38+
for qubit in reversed(range(n)):
39+
for target in reversed(range(qubit + 1, n)):
40+
qc.crz(-np.pi / 2 ** (target - qubit), qubit, target)
41+
qc.h(qubit)
42+
43+
qc.measure_all()
44+
45+
job = backend.run(qc, shots=200, qasm3=False, queued=True)
46+
counts = job.result().get_counts()
47+
print("results:", counts)
48+
49+
plot_histogram(counts)
50+
plt.show()

β€Žexamples/shor_period_find.pyβ€Ž

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Shor period finding example
2+
# Find the period of f(x) = 13^x mod 15
3+
4+
import math
5+
import os
6+
7+
import matplotlib.pyplot as plt
8+
from dotenv import load_dotenv
9+
from mqss.qiskit_adapter import MQSSQiskitAdapter
10+
from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister
11+
12+
# Load environment variables
13+
load_dotenv()
14+
token = os.getenv("MQP_TOKEN")
15+
backend_name = os.getenv("MQP_BACKEND")
16+
17+
adapter = MQSSQiskitAdapter(token=token)
18+
backend = adapter.get_backend(backend_name)
19+
20+
# Parameters
21+
n = 4 # number of qubits in x-register
22+
shots = 200
23+
24+
25+
def f(x):
26+
return pow(13, x, 15)
27+
28+
29+
# Build circuit
30+
qr_x = QuantumRegister(n, "x")
31+
qr_fx = QuantumRegister(4, "f(x)")
32+
cr_x = ClassicalRegister(n, "c_x")
33+
qc = QuantumCircuit(qr_x, qr_fx, cr_x)
34+
35+
# Superposition
36+
for q in range(n):
37+
qc.h(qr_x[q])
38+
qc.barrier()
39+
40+
# f(x) implementation
41+
qc.x(qr_fx[0])
42+
qc.x(qr_fx[2])
43+
qc.x(qr_x[0])
44+
qc.ccx(qr_x[0], qr_x[1], qr_fx[0])
45+
qc.x(qr_x[0])
46+
qc.ccx(qr_x[0], qr_x[1], qr_fx[1])
47+
qc.x(qr_x[0])
48+
qc.x(qr_x[1])
49+
qc.ccx(qr_x[0], qr_x[1], qr_fx[2])
50+
qc.x(qr_x[0])
51+
qc.ccx(qr_x[0], qr_x[1], qr_fx[3])
52+
qc.x(qr_x[1])
53+
qc.barrier()
54+
55+
# QFT
56+
for q in range(n - 1, -1, -1):
57+
qc.h(q)
58+
for k in range(1, q + 1):
59+
qc.cp(math.pi / 2**k, q - k, q)
60+
qc.barrier()
61+
for q in range(n // 2):
62+
qc.swap(q, (n - 1) - q)
63+
qc.barrier()
64+
65+
qc.measure(qr_x, cr_x)
66+
67+
# Run job
68+
job = backend.run(qc, shots=shots, queued=True)
69+
counts = job.result().get_counts()
70+
print("Results:", counts)
71+
72+
# Normalize and plot
73+
keys = list(counts.keys())
74+
x_vals = [int(k, 2) for k in keys]
75+
probs = [v / sum(counts.values()) for v in counts.values()]
76+
77+
x_sorted, p_sorted = zip(*sorted(zip(x_vals, probs)))
78+
79+
plt.figure()
80+
plt.grid(zorder=0, axis="y", linestyle="--")
81+
plt.bar(x_sorted, p_sorted, zorder=3)
82+
plt.xticks(rotation=65)
83+
plt.ylabel("Probability")
84+
plt.xlabel("x")
85+
plt.show()

β€Žpyproject.tomlβ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,6 @@ dev = [
6868
"mkdocstrings[python]",
6969
"ruff>=0.11.5",
7070
"pytest-cov>=6.1.1",
71+
"matplotlib",
72+
"python-dotenv",
7173
]

0 commit comments

Comments
Β (0)