From ed6f0c7cbc2110bbbb4b8c5ab24e8dab2ce4aff0 Mon Sep 17 00:00:00 2001 From: Marco De Pascale Date: Fri, 17 Oct 2025 09:46:49 +0200 Subject: [PATCH 1/9] =?UTF-8?q?=F0=9F=9A=B8=20Add=20examples?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/GHZ_measure.py | 49 ++++++++++++++++ examples/inverse_unitary_test.py | 46 +++++++++++++++ examples/shor_period_find.py | 99 ++++++++++++++++++++++++++++++++ 3 files changed, 194 insertions(+) create mode 100644 examples/GHZ_measure.py create mode 100644 examples/inverse_unitary_test.py create mode 100644 examples/shor_period_find.py diff --git a/examples/GHZ_measure.py b/examples/GHZ_measure.py new file mode 100644 index 0000000..4e3bca8 --- /dev/null +++ b/examples/GHZ_measure.py @@ -0,0 +1,49 @@ +# GHZ State generation and measurement + +# Load packages + connect to device + +from mqss.qiskit_adapter import MQSSQiskitAdapter +from qiskit import QuantumCircuit, transpile + +token = "" +adapter = MQSSQiskitAdapter(token=token) +[backend] = adapter.backends(name="") + +# Set Parameters + +# Pay attention `shots <= 200` for AQT20 + +# set number of qubits and measurement shots for the quantum circuit +qubits = 8 +shots = 200 +# Repetitions +rep = 2 +meas = [0] + +# Generate circuit + +qc = QuantumCircuit(qubits, qubits) +qc.h(0) +for i in range(1, qubits): + qc.cx(0, i) +qc.measure_all(add_bits=False) + +# Transpile using Qiskit 'transpile' +# Comment out the these two lines if you are running on QLM +trans_qc = transpile(qc, backend, optimization_level=3) +job = backend.run(trans_qc, shots=shots, qasm3=True, queued=True) +# print(trans_qc) + +# Run circuit on hardware + +# ------- Use this if running on QLM backend --------- +# job = backend.run(qc, shots=shots, qasm3=False, queued=True) + +counts = job.result().get_counts() +print("Result: ", counts) + +# Visualize Results + +from qiskit.visualization import plot_histogram + +plot_histogram(counts, figsize=(12, 6)) diff --git a/examples/inverse_unitary_test.py b/examples/inverse_unitary_test.py new file mode 100644 index 0000000..5a143ad --- /dev/null +++ b/examples/inverse_unitary_test.py @@ -0,0 +1,46 @@ +# Inverse Unitary Test +# We put our initial state to |011> and then apply unitaries. +# Then we apply the unitaries again in reverse and should get the state |001> again. +# This example highlights the difference between running a problem on a noiseless simulator vs an actual hardware + +from mqss.qiskit_adapter import MQSSQiskitAdapter +from qiskit import QuantumCircuit +import numpy as np + +# Connect to MQSS +adapter = MQSSQiskitAdapter("") +backend = adapter.get_backend("") + +n = 3 # number of qubits +x = 3 # input state |011> + +qc = QuantumCircuit(n) + +# Prepare basis state |x⟩ +for i in range(n): + if (x >> i) & 1: + qc.x(i) + +# --- Unitaries --- +for qubit in range(n): + qc.h(qubit) + for target in range(qubit + 1, n): + qc.crz(np.pi / 2 ** (target - qubit), qubit, target) + +# --- Inverse Unitaries --- +for qubit in reversed(range(n)): + for target in reversed(range(qubit + 1, n)): + qc.crz(-np.pi / 2 ** (target - qubit), qubit, target) + qc.h(qubit) + +qc.measure_all() + +# Run the job +job = backend.run(qc, shots=200, qasm3=False, queued=True) +counts = job.result().get_counts() +print("results:", counts) + +# Visualization +from qiskit.visualization import plot_histogram + +plot_histogram(counts) diff --git a/examples/shor_period_find.py b/examples/shor_period_find.py new file mode 100644 index 0000000..fa78a2e --- /dev/null +++ b/examples/shor_period_find.py @@ -0,0 +1,99 @@ +# Shor period finding example + +# Find period of **f(x) = 13^x mod 15** + +# load packages and enable backend + +from mqss.qiskit_adapter import MQSSQiskitAdapter +from qiskit import QuantumCircuit, transpile, QuantumRegister, ClassicalRegister + +import math +import matplotlib.pyplot as plt + +token = "" +adapter = MQSSQiskitAdapter(token=token) +[backend] = adapter.backends(name="=2 must hold +shots = 200 # number of shots per run + +# Define function + + +def f(x): + return pow(13, x, 15) + + +# Generate circuit + +qr_x = QuantumRegister(n, "x") +qr_fx = QuantumRegister(4, "f(x)") +cr_x = ClassicalRegister(n, "c_x") +qc = QuantumCircuit(qr_x, qr_fx, cr_x) +# equal superposition +for q in range(n): + qc.h(qr_x[q]) +qc.barrier() +# f(x) = 13^x mod 15 +qc.x(qr_fx[0]) +qc.x(qr_fx[2]) +qc.x(qr_x[0]) +qc.ccx(qr_x[0], qr_x[1], qr_fx[0]) +qc.x(qr_x[0]) +qc.ccx(qr_x[0], qr_x[1], qr_fx[1]) +qc.x(qr_x[0]) +qc.x(qr_x[1]) +qc.ccx(qr_x[0], qr_x[1], qr_fx[2]) +qc.x(qr_x[0]) +qc.ccx(qr_x[0], qr_x[1], qr_fx[3]) +qc.x(qr_x[1]) +qc.barrier() +# QFT +for q in range(n - 1, -1, -1): + qc.h(q) + for k in range(1, q + 1): + qc.cp(math.pi / 2**k, q - k, q) + qc.barrier() +for q in range(n // 2): + qc.swap(q, (n - 1) - q) +qc.barrier() + +# This should ALWAYS give k*2^n/4, where k>=0 is a random integer. +# It's because the period of f(x) is 4, which is a power of 2. +qc.measure(qr_x, cr_x) + +# Draw circuit + +qc.draw(scale=0.5, output="mpl") + +# Run circuit on hardware + +result = backend.run(qc, shots=shots, optimization_level=3).result() + +counts = result.get_counts() + +print(f"Result counts: {counts}") # e.g. for n=4 1100 is 12, which is 3*2^4/4 + +# Plotting the results + +# plotting: +x = list(counts.keys()) +x_values = [] +for item in x: + x_values.append(int(item, 2)) +y = list(counts.values()) +y = [i / sum(y) for i in y] + +x_values_sorted, y_sorted = zip(*sorted(zip(x_values, y))) + +fig = plt.figure() +plt.grid(zorder=0, axis="y", linestyle="--") +plt.bar(x=x, height=y_sorted, zorder=3) +locs, labels = plt.xticks() +plt.xticks(locs, x_values_sorted, rotation=65) +plt.ylabel("Probabilities") +plt.xlabel("x") + +plt.show() From 4f89ecd2385a0c53576f7dd6fc1adfdb8187c9d8 Mon Sep 17 00:00:00 2001 From: Marco De Pascale Date: Tue, 21 Oct 2025 11:26:22 +0200 Subject: [PATCH 2/9] =?UTF-8?q?=F0=9F=90=9B=20Imports=20all=20on=20top?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/GHZ_measure.py | 2 +- examples/inverse_unitary_test.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/GHZ_measure.py b/examples/GHZ_measure.py index 4e3bca8..71af6c9 100644 --- a/examples/GHZ_measure.py +++ b/examples/GHZ_measure.py @@ -4,6 +4,7 @@ from mqss.qiskit_adapter import MQSSQiskitAdapter from qiskit import QuantumCircuit, transpile +from qiskit.visualization import plot_histogram token = "" adapter = MQSSQiskitAdapter(token=token) @@ -44,6 +45,5 @@ # Visualize Results -from qiskit.visualization import plot_histogram plot_histogram(counts, figsize=(12, 6)) diff --git a/examples/inverse_unitary_test.py b/examples/inverse_unitary_test.py index 5a143ad..87970f0 100644 --- a/examples/inverse_unitary_test.py +++ b/examples/inverse_unitary_test.py @@ -7,6 +7,9 @@ from qiskit import QuantumCircuit import numpy as np +# Visualization +from qiskit.visualization import plot_histogram + # Connect to MQSS adapter = MQSSQiskitAdapter("") backend = adapter.get_backend("") @@ -40,7 +43,5 @@ counts = job.result().get_counts() print("results:", counts) -# Visualization -from qiskit.visualization import plot_histogram plot_histogram(counts) From abb3eb15fa5f202c0fe87c8b146d4089b47553b1 Mon Sep 17 00:00:00 2001 From: Marco De Pascale Date: Tue, 21 Oct 2025 11:42:35 +0200 Subject: [PATCH 3/9] :bug: dummy commit --- examples/shor_period_find.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/shor_period_find.py b/examples/shor_period_find.py index fa78a2e..20c20c8 100644 --- a/examples/shor_period_find.py +++ b/examples/shor_period_find.py @@ -14,7 +14,8 @@ adapter = MQSSQiskitAdapter(token=token) [backend] = adapter.backends(name="=2 must hold shots = 200 # number of shots per run From ed9e1b42b2276fbb88d631ecbe91063a8bdb8d29 Mon Sep 17 00:00:00 2001 From: Marco De Pascale Date: Tue, 21 Oct 2025 11:46:26 +0200 Subject: [PATCH 4/9] =?UTF-8?q?=F0=9F=90=9B=20Removed=20unused=20imported?= =?UTF-8?q?=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/shor_period_find.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/shor_period_find.py b/examples/shor_period_find.py index 20c20c8..e9d8409 100644 --- a/examples/shor_period_find.py +++ b/examples/shor_period_find.py @@ -5,7 +5,7 @@ # load packages and enable backend from mqss.qiskit_adapter import MQSSQiskitAdapter -from qiskit import QuantumCircuit, transpile, QuantumRegister, ClassicalRegister +from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister import math import matplotlib.pyplot as plt @@ -15,7 +15,7 @@ [backend] = adapter.backends(name="=2 must hold shots = 200 # number of shots per run From 289a359df1c0de70bf3ed47f6f0b9a72508deb18 Mon Sep 17 00:00:00 2001 From: MShahzaib Date: Wed, 12 Nov 2025 16:38:15 +0100 Subject: [PATCH 5/9] =?UTF-8?q?=F0=9F=94=A8=20Add=20matplotlib=20as=20dev?= =?UTF-8?q?=20dependency=20for=20example=20tests?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyproject.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/pyproject.toml b/pyproject.toml index dbd4e24..26657a2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -68,4 +68,5 @@ dev = [ "mkdocstrings[python]", "ruff>=0.11.5", "pytest-cov>=6.1.1", + "matplotlib", ] From 24cfcca4050beefe88297630145cf878c2b46951 Mon Sep 17 00:00:00 2001 From: MShahzaib Date: Wed, 12 Nov 2025 16:40:49 +0100 Subject: [PATCH 6/9] =?UTF-8?q?=F0=9F=91=B7=20updated=20CI.yml=20to=20inst?= =?UTF-8?q?all=20dev=20dependencies?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 07a348b..05d5b2e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,7 +30,7 @@ jobs: python -m pip install --upgrade pip pip install nox pip install uv - uv sync + uv sync --group dev - name: Run pre-commit run: | uv run pre-commit install From 5eb03cb0539f471e1cac02f65aa3aae5be81c777 Mon Sep 17 00:00:00 2001 From: MShahzaib Date: Mon, 17 Nov 2025 15:13:22 +0100 Subject: [PATCH 7/9] =?UTF-8?q?=E2=9C=A8=20Add=20example=20scripts=20using?= =?UTF-8?q?=20dotenv?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/GHZ_measure.py | 23 +++++++++++++++-------- examples/inverse_unitary_test.py | 25 +++++++++++++++++-------- examples/shor_period_find.py | 29 +++++++++++++++-------------- pyproject.toml | 1 + 4 files changed, 48 insertions(+), 30 deletions(-) diff --git a/examples/GHZ_measure.py b/examples/GHZ_measure.py index 71af6c9..f83da8f 100644 --- a/examples/GHZ_measure.py +++ b/examples/GHZ_measure.py @@ -1,20 +1,27 @@ # GHZ State generation and measurement # Load packages + connect to device - +import os from mqss.qiskit_adapter import MQSSQiskitAdapter from qiskit import QuantumCircuit, transpile from qiskit.visualization import plot_histogram +import matplotlib.pyplot as plt +from dotenv import load_dotenv -token = "" -adapter = MQSSQiskitAdapter(token=token) -[backend] = adapter.backends(name="") +# Loading variables from the .env file -# Set Parameters +load_dotenv() # reads .env if present +token = os.getenv("MQP_TOKEN") +backend_name = os.getenv("MQP_BACKEND") -# Pay attention `shots <= 200` for AQT20 +# Initialize MQSS adapter and get backend + +adapter = MQSSQiskitAdapter(token=token) +backend = adapter.get_backend(backend_name) +# Pay attention `shots <= 200` for AQT20 # set number of qubits and measurement shots for the quantum circuit + qubits = 8 shots = 200 # Repetitions @@ -32,7 +39,7 @@ # Transpile using Qiskit 'transpile' # Comment out the these two lines if you are running on QLM trans_qc = transpile(qc, backend, optimization_level=3) -job = backend.run(trans_qc, shots=shots, qasm3=True, queued=True) +job = backend.run(trans_qc, no_modify=False, shots=shots, qasm3=True, queued=True) # print(trans_qc) # Run circuit on hardware @@ -45,5 +52,5 @@ # Visualize Results - plot_histogram(counts, figsize=(12, 6)) +plt.show() \ No newline at end of file diff --git a/examples/inverse_unitary_test.py b/examples/inverse_unitary_test.py index 87970f0..ebe325b 100644 --- a/examples/inverse_unitary_test.py +++ b/examples/inverse_unitary_test.py @@ -1,18 +1,26 @@ # Inverse Unitary Test # We put our initial state to |011> and then apply unitaries. -# Then we apply the unitaries again in reverse and should get the state |001> again. -# This example highlights the difference between running a problem on a noiseless simulator vs an actual hardware +# Then we apply the unitaries again in reverse and should get the state |011> in a noiseless system but on a real hardware we would likely get some hits in the other states too. +# This example highlights the difference between running a problem on a noiseless simulator (QLM) vs an actual hardware (e.g., QExa20) from mqss.qiskit_adapter import MQSSQiskitAdapter +import os +from dotenv import load_dotenv from qiskit import QuantumCircuit import numpy as np - -# Visualization +import matplotlib.pyplot as plt from qiskit.visualization import plot_histogram -# Connect to MQSS -adapter = MQSSQiskitAdapter("") -backend = adapter.get_backend("") +# Loading variables from the .env file + +load_dotenv() # reads .env if present +token = os.getenv("MQP_TOKEN") +backend_name = os.getenv("MQP_BACKEND") + +# Initialize MQSS adapter and get backend + +adapter = MQSSQiskitAdapter(token=token) +backend = adapter.get_backend(backend_name) n = 3 # number of qubits x = 3 # input state |011> @@ -43,5 +51,6 @@ counts = job.result().get_counts() print("results:", counts) - +# Visualization plot_histogram(counts) +plt.show() \ No newline at end of file diff --git a/examples/shor_period_find.py b/examples/shor_period_find.py index e9d8409..fb13296 100644 --- a/examples/shor_period_find.py +++ b/examples/shor_period_find.py @@ -1,19 +1,25 @@ # Shor period finding example - # Find period of **f(x) = 13^x mod 15** # load packages and enable backend from mqss.qiskit_adapter import MQSSQiskitAdapter from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister - +import os +from dotenv import load_dotenv import math import matplotlib.pyplot as plt -token = "" -adapter = MQSSQiskitAdapter(token=token) -[backend] = adapter.backends(name="=0.11.5", "pytest-cov>=6.1.1", "matplotlib", + "python-dotenv", ] From 834adef402725208e5d0468b70c8e345777090a5 Mon Sep 17 00:00:00 2001 From: MShahzaib Date: Wed, 19 Nov 2025 09:59:12 +0100 Subject: [PATCH 8/9] =?UTF-8?q?=F0=9F=9A=A7=20add=20example=20scripts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/GHZ_measure.py | 42 ++++++------------ examples/inverse_unitary_test.py | 32 ++++++-------- examples/shor_period_find.py | 74 +++++++++++++------------------- 3 files changed, 55 insertions(+), 93 deletions(-) diff --git a/examples/GHZ_measure.py b/examples/GHZ_measure.py index f83da8f..d38fdcf 100644 --- a/examples/GHZ_measure.py +++ b/examples/GHZ_measure.py @@ -1,56 +1,40 @@ # GHZ State generation and measurement -# Load packages + connect to device import os + +import matplotlib.pyplot as plt +from dotenv import load_dotenv from mqss.qiskit_adapter import MQSSQiskitAdapter from qiskit import QuantumCircuit, transpile from qiskit.visualization import plot_histogram -import matplotlib.pyplot as plt -from dotenv import load_dotenv - -# Loading variables from the .env file -load_dotenv() # reads .env if present +# Load environment variables +load_dotenv() token = os.getenv("MQP_TOKEN") backend_name = os.getenv("MQP_BACKEND") -# Initialize MQSS adapter and get backend - adapter = MQSSQiskitAdapter(token=token) backend = adapter.get_backend(backend_name) -# Pay attention `shots <= 200` for AQT20 -# set number of qubits and measurement shots for the quantum circuit - +# Parameters qubits = 8 shots = 200 -# Repetitions -rep = 2 -meas = [0] - -# Generate circuit +# Build GHZ circuit qc = QuantumCircuit(qubits, qubits) qc.h(0) for i in range(1, qubits): qc.cx(0, i) qc.measure_all(add_bits=False) -# Transpile using Qiskit 'transpile' -# Comment out the these two lines if you are running on QLM +# Transpile if needed trans_qc = transpile(qc, backend, optimization_level=3) -job = backend.run(trans_qc, no_modify=False, shots=shots, qasm3=True, queued=True) -# print(trans_qc) - -# Run circuit on hardware - -# ------- Use this if running on QLM backend --------- -# job = backend.run(qc, shots=shots, qasm3=False, queued=True) +# Run job +job = backend.run(trans_qc, no_modify=False, shots=shots, qasm3=True, queued=True) counts = job.result().get_counts() -print("Result: ", counts) - -# Visualize Results +print("Result:", counts) +# Plot plot_histogram(counts, figsize=(12, 6)) -plt.show() \ No newline at end of file +plt.show() diff --git a/examples/inverse_unitary_test.py b/examples/inverse_unitary_test.py index ebe325b..1b7883a 100644 --- a/examples/inverse_unitary_test.py +++ b/examples/inverse_unitary_test.py @@ -1,44 +1,40 @@ # Inverse Unitary Test -# We put our initial state to |011> and then apply unitaries. -# Then we apply the unitaries again in reverse and should get the state |011> in a noiseless system but on a real hardware we would likely get some hits in the other states too. -# This example highlights the difference between running a problem on a noiseless simulator (QLM) vs an actual hardware (e.g., QExa20) +# Demonstrates how reversible a circuit is on simulator vs real hardware. -from mqss.qiskit_adapter import MQSSQiskitAdapter import os + +import matplotlib.pyplot as plt +import numpy as np from dotenv import load_dotenv +from mqss.qiskit_adapter import MQSSQiskitAdapter from qiskit import QuantumCircuit -import numpy as np -import matplotlib.pyplot as plt from qiskit.visualization import plot_histogram -# Loading variables from the .env file - -load_dotenv() # reads .env if present +# Load environment variables +load_dotenv() token = os.getenv("MQP_TOKEN") backend_name = os.getenv("MQP_BACKEND") -# Initialize MQSS adapter and get backend - adapter = MQSSQiskitAdapter(token=token) backend = adapter.get_backend(backend_name) -n = 3 # number of qubits -x = 3 # input state |011> +n = 3 +x = 3 # |011⟩ qc = QuantumCircuit(n) -# Prepare basis state |x⟩ +# Prepare |x⟩ for i in range(n): if (x >> i) & 1: qc.x(i) -# --- Unitaries --- +# Unitaries for qubit in range(n): qc.h(qubit) for target in range(qubit + 1, n): qc.crz(np.pi / 2 ** (target - qubit), qubit, target) -# --- Inverse Unitaries --- +# Inverse unitaries for qubit in reversed(range(n)): for target in reversed(range(qubit + 1, n)): qc.crz(-np.pi / 2 ** (target - qubit), qubit, target) @@ -46,11 +42,9 @@ qc.measure_all() -# Run the job job = backend.run(qc, shots=200, qasm3=False, queued=True) counts = job.result().get_counts() print("results:", counts) -# Visualization plot_histogram(counts) -plt.show() \ No newline at end of file +plt.show() diff --git a/examples/shor_period_find.py b/examples/shor_period_find.py index fb13296..99975a5 100644 --- a/examples/shor_period_find.py +++ b/examples/shor_period_find.py @@ -1,47 +1,43 @@ # Shor period finding example -# Find period of **f(x) = 13^x mod 15** +# Find the period of f(x) = 13^x mod 15 -# load packages and enable backend - -from mqss.qiskit_adapter import MQSSQiskitAdapter -from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister -import os -from dotenv import load_dotenv import math -import matplotlib.pyplot as plt +import os -# Loading variables from the .env file +import matplotlib.pyplot as plt +from dotenv import load_dotenv +from mqss.qiskit_adapter import MQSSQiskitAdapter +from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister -load_dotenv() # reads .env if present +# Load environment variables +load_dotenv() token = os.getenv("MQP_TOKEN") backend_name = os.getenv("MQP_BACKEND") -# Initialize MQSS adapter and get backend - adapter = MQSSQiskitAdapter(token=token) backend = adapter.get_backend(backend_name) -# Set parameters +# Parameters +n = 4 # number of qubits in x-register +shots = 200 -n = 4 # x is an n-bit number; feel free to change it, n>=2 must hold -shots = 200 # number of shots per run - -# Define function def f(x): return pow(13, x, 15) -# Generate circuit +# Build circuit qr_x = QuantumRegister(n, "x") qr_fx = QuantumRegister(4, "f(x)") cr_x = ClassicalRegister(n, "c_x") qc = QuantumCircuit(qr_x, qr_fx, cr_x) -# equal superposition + +# Superposition for q in range(n): qc.h(qr_x[q]) qc.barrier() -# f(x) = 13^x mod 15 + +# f(x) implementation qc.x(qr_fx[0]) qc.x(qr_fx[2]) qc.x(qr_x[0]) @@ -55,6 +51,7 @@ def f(x): qc.ccx(qr_x[0], qr_x[1], qr_fx[3]) qc.x(qr_x[1]) qc.barrier() + # QFT for q in range(n - 1, -1, -1): qc.h(q) @@ -65,37 +62,24 @@ def f(x): qc.swap(q, (n - 1) - q) qc.barrier() -# This should ALWAYS give k*2^n/4, where k>=0 is a random integer. -# It's because the period of f(x) is 4, which is a power of 2. qc.measure(qr_x, cr_x) -# Draw circuit - -qc.draw(scale=0.5, output="mpl") - -# Run circuit on hardware - -job = backend.run(qc, shots=200, qasm3=True, queued=True) +# Run job +job = backend.run(qc, shots=shots, qasm3=True, queued=True) counts = job.result().get_counts() -print("results:", counts) # e.g. for n=4 1100 is 12, which is 3*2^4/4 +print("Results:", counts) -# Visualization +# Normalize and plot +keys = list(counts.keys()) +x_vals = [int(k, 2) for k in keys] +probs = [v / sum(counts.values()) for v in counts.values()] -x = list(counts.keys()) -x_values = [] -for item in x: - x_values.append(int(item, 2)) -y = list(counts.values()) -y = [i / sum(y) for i in y] +x_sorted, p_sorted = zip(*sorted(zip(x_vals, probs))) -x_values_sorted, y_sorted = zip(*sorted(zip(x_values, y))) - -fig = plt.figure() +plt.figure() plt.grid(zorder=0, axis="y", linestyle="--") -plt.bar(x=x, height=y_sorted, zorder=3) -locs, labels = plt.xticks() -plt.xticks(locs, x_values_sorted, rotation=65) -plt.ylabel("Probabilities") +plt.bar(x_sorted, p_sorted, zorder=3) +plt.xticks(rotation=65) +plt.ylabel("Probability") plt.xlabel("x") - plt.show() From 2554e4192f1389d96ab8f03fd8162e9f8854883c Mon Sep 17 00:00:00 2001 From: MShahzaib Date: Thu, 12 Feb 2026 09:44:49 +0100 Subject: [PATCH 9/9] =?UTF-8?q?=F0=9F=9A=A7Address=20PR=20comments=20and?= =?UTF-8?q?=20update=20examples?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/GHZ_measure.py | 2 +- examples/shor_period_find.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/GHZ_measure.py b/examples/GHZ_measure.py index d38fdcf..441ceba 100644 --- a/examples/GHZ_measure.py +++ b/examples/GHZ_measure.py @@ -31,7 +31,7 @@ trans_qc = transpile(qc, backend, optimization_level=3) # Run job -job = backend.run(trans_qc, no_modify=False, shots=shots, qasm3=True, queued=True) +job = backend.run(trans_qc, no_modify=True, shots=shots, queued=True) counts = job.result().get_counts() print("Result:", counts) diff --git a/examples/shor_period_find.py b/examples/shor_period_find.py index 99975a5..0380a37 100644 --- a/examples/shor_period_find.py +++ b/examples/shor_period_find.py @@ -65,7 +65,7 @@ def f(x): qc.measure(qr_x, cr_x) # Run job -job = backend.run(qc, shots=shots, qasm3=True, queued=True) +job = backend.run(qc, shots=shots, queued=True) counts = job.result().get_counts() print("Results:", counts)