mirror of
https://github.com/kristoferssolo/grovers-visualizer.git
synced 2025-10-21 20:10:35 +00:00
feat: implement dynamic graphic updates
This commit is contained in:
parent
95332c5d84
commit
edddeec89e
62
main.py
62
main.py
@ -6,28 +6,70 @@ simulation using Qiskit's Aer simulator, and visualizes the results
|
||||
using matplotlib.
|
||||
"""
|
||||
|
||||
from itertools import product
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib.axes import Axes
|
||||
from qiskit import QuantumCircuit
|
||||
from qiskit_aer import AerSimulator
|
||||
|
||||
|
||||
def grover_search(n: int) -> QuantumCircuit:
|
||||
qc = QuantumCircuit(n, n)
|
||||
|
||||
qc.h(range(n))
|
||||
|
||||
qc.measure(range(n), range(n))
|
||||
return qc
|
||||
|
||||
|
||||
def plot_counts(ax: Axes, counts: dict[str, int], target_state: str) -> None:
|
||||
"""Create and display a bar chart for the measurement results.
|
||||
|
||||
Parameters:
|
||||
counts - A dictionary mapping output states to counts.
|
||||
target_state - The target state used in the Grover circuit.
|
||||
"""
|
||||
# Sort the states (optional: you can sort by state or by count)
|
||||
states = list(counts.keys())
|
||||
frequencies = [counts[s] for s in states]
|
||||
|
||||
ax.clear()
|
||||
ax.bar(states, frequencies, color="skyblue")
|
||||
ax.set_xlabel("Measured State")
|
||||
ax.set_ylabel("Counts")
|
||||
ax.set_title(f"Measurement Counts for Target: {target_state}")
|
||||
ax.set_ylim(0, max(frequencies) * 1.2)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
qc = QuantumCircuit(1, 1)
|
||||
n_qubits = 3
|
||||
combinations = product(["0", "1"], repeat=n_qubits)
|
||||
states = ["".join(x) for x in combinations]
|
||||
shots = 1024
|
||||
|
||||
qc.h(0)
|
||||
_, ax = plt.subplots(figsize=(8, 4))
|
||||
plt.ion()
|
||||
|
||||
qc.measure(0, 0)
|
||||
for state in states:
|
||||
qc = grover_search(n_qubits)
|
||||
|
||||
print("Circuit Diagram:")
|
||||
print(qc.draw("text"))
|
||||
print(qc.draw("text"))
|
||||
|
||||
sim = AerSimulator()
|
||||
simulator = AerSimulator()
|
||||
job = simulator.run(qc, shots=shots)
|
||||
result = job.result()
|
||||
counts: dict[str, int] = result.get_counts(qc)
|
||||
sorted_counts = dict(sorted(counts.items(), key=lambda x: x[1], reverse=True))
|
||||
|
||||
job = sim.run(qc, shots=1024)
|
||||
results = job.result()
|
||||
print(f"Target: {state}")
|
||||
print("\n".join(f"'{k}': {v}" for k, v in sorted_counts.items()))
|
||||
|
||||
counts = results.get_counts()
|
||||
plot_counts(ax, sorted_counts, state)
|
||||
plt.pause(1)
|
||||
|
||||
print(f"\nResults: {counts}")
|
||||
# plt.ioff() # Do not close automatically
|
||||
plt.show()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@ -5,12 +5,14 @@ description = "Add your description here"
|
||||
readme = "README.md"
|
||||
requires-python = ">=3.13"
|
||||
dependencies = [
|
||||
"matplotlib>=3.10.1",
|
||||
"numpy>=2.2.4",
|
||||
"qiskit-aer>=0.17.0",
|
||||
"qiskit[visualization]>=2.0.0",
|
||||
]
|
||||
|
||||
[project.optional-dependencies]
|
||||
mpl = ["pyqt6>=6.9.0"]
|
||||
|
||||
[dependency-groups]
|
||||
dev = ["mypy>=1.15.0", "ruff>=0.11.4"]
|
||||
|
||||
|
||||
51
uv.lock
51
uv.lock
@ -72,12 +72,16 @@ name = "grovers-visualizer"
|
||||
version = "0.1.0"
|
||||
source = { virtual = "." }
|
||||
dependencies = [
|
||||
{ name = "matplotlib" },
|
||||
{ name = "numpy" },
|
||||
{ name = "qiskit", extra = ["visualization"] },
|
||||
{ name = "qiskit-aer" },
|
||||
]
|
||||
|
||||
[package.optional-dependencies]
|
||||
mpl = [
|
||||
{ name = "pyqt6" },
|
||||
]
|
||||
|
||||
[package.dev-dependencies]
|
||||
dev = [
|
||||
{ name = "mypy" },
|
||||
@ -86,8 +90,8 @@ dev = [
|
||||
|
||||
[package.metadata]
|
||||
requires-dist = [
|
||||
{ name = "matplotlib", specifier = ">=3.10.1" },
|
||||
{ name = "numpy", specifier = ">=2.2.4" },
|
||||
{ name = "pyqt6", marker = "extra == 'mpl'", specifier = ">=6.9.0" },
|
||||
{ name = "qiskit", extras = ["visualization"], specifier = ">=2.0.0" },
|
||||
{ name = "qiskit-aer", specifier = ">=0.17.0" },
|
||||
]
|
||||
@ -347,6 +351,49 @@ wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/05/e7/df2285f3d08fee213f2d041540fa4fc9ca6c2d44cf36d3a035bf2a8d2bcc/pyparsing-3.2.3-py3-none-any.whl", hash = "sha256:a749938e02d6fd0b59b356ca504a24982314bb090c383e3cf201c95ef7e2bfcf", size = 111120 },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyqt6"
|
||||
version = "6.9.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
dependencies = [
|
||||
{ name = "pyqt6-qt6" },
|
||||
{ name = "pyqt6-sip" },
|
||||
]
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/32/de/102e8e66149085acf38bbf01df572a2cd53259bcd99b7d8ecef0d6b36172/pyqt6-6.9.0.tar.gz", hash = "sha256:6a8ff8e3cd18311bb7d937f7d741e787040ae7ff47ce751c28a94c5cddc1b4e6", size = 1066831 }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/97/e5/f9e2b5326d6103bce4894a969be54ce3be4b0a7a6ff848228e6a61a9993f/PyQt6-6.9.0-cp39-abi3-macosx_10_14_universal2.whl", hash = "sha256:5344240747e81bde1a4e0e98d4e6e2d96ad56a985d8f36b69cd529c1ca9ff760", size = 12257215 },
|
||||
{ url = "https://files.pythonhosted.org/packages/ed/3a/bcc7687c5a11079bbd1606a015514562f2ac8cb01c5e3e4a3b30fcbdad36/PyQt6-6.9.0-cp39-abi3-manylinux_2_28_x86_64.whl", hash = "sha256:e344868228c71fc89a0edeb325497df4ff731a89cfa5fe57a9a4e9baecc9512b", size = 8259731 },
|
||||
{ url = "https://files.pythonhosted.org/packages/e1/47/13ab0b916b5bad07ab04767b412043f5c1ca206bf38a906b1d8d5c520a98/PyQt6-6.9.0-cp39-abi3-manylinux_2_39_aarch64.whl", hash = "sha256:1cbc5a282454cf19691be09eadbde019783f1ae0523e269b211b0173b67373f6", size = 8207593 },
|
||||
{ url = "https://files.pythonhosted.org/packages/d1/a8/955cfd880f2725a218ee7b272c005658e857e9224823d49c32c93517f6d9/PyQt6-6.9.0-cp39-abi3-win_amd64.whl", hash = "sha256:d36482000f0cd7ce84a35863766f88a5e671233d5f1024656b600cd8915b3752", size = 6748279 },
|
||||
{ url = "https://files.pythonhosted.org/packages/9f/38/586ce139b1673a27607f7b85c594878e1bba215abdca3de67732b463f7b2/PyQt6-6.9.0-cp39-abi3-win_arm64.whl", hash = "sha256:0c8b7251608e05b479cfe731f95857e853067459f7cbbcfe90f89de1bcf04280", size = 5478122 },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyqt6-qt6"
|
||||
version = "6.9.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/e2/11/8c450442bf4702ed810689a045f9c5d9236d709163886f09374fd8d84143/PyQt6_Qt6-6.9.0-py3-none-macosx_10_14_x86_64.whl", hash = "sha256:b1c4e4a78f0f22fbf88556e3d07c99e5ce93032feae5c1e575958d914612e0f9", size = 66804297 },
|
||||
{ url = "https://files.pythonhosted.org/packages/6e/be/191ba4402c24646f6b98c326ff0ee22e820096c69e67ba5860a687057616/PyQt6_Qt6-6.9.0-py3-none-macosx_11_0_arm64.whl", hash = "sha256:6d3875119dec6bf5f799facea362aa0ad39bb23aa9654112faa92477abccb5ff", size = 60943708 },
|
||||
{ url = "https://files.pythonhosted.org/packages/0f/70/ec018b6e979b3914c984e5ab7e130918930d5423735ac96c70c328227b9b/PyQt6_Qt6-6.9.0-py3-none-manylinux_2_28_x86_64.whl", hash = "sha256:9c0e603c934e4f130c110190fbf2c482ff1221a58317266570678bc02db6b152", size = 81846956 },
|
||||
{ url = "https://files.pythonhosted.org/packages/ac/ed/2d78cd08be415a21dac2e7277967b90b0c05afc4782100f0a037447bb1c6/PyQt6_Qt6-6.9.0-py3-none-manylinux_2_39_aarch64.whl", hash = "sha256:cf840e8ae20a0704e0343810cf0e485552db28bf09ea976e58ec0e9b7bb27fcd", size = 80295982 },
|
||||
{ url = "https://files.pythonhosted.org/packages/6e/24/6b6168a75c7b6a55b9f6b5c897e6164ec15e94594af11a6f358c49845442/PyQt6_Qt6-6.9.0-py3-none-win_amd64.whl", hash = "sha256:c825a6f5a9875ef04ef6681eda16aa3a9e9ad71847aa78dfafcf388c8007aa0a", size = 73652485 },
|
||||
{ url = "https://files.pythonhosted.org/packages/44/fd/1238931df039e46e128d53974c0cfc9d34da3d54c5662bd589fe7b0a67c2/PyQt6_Qt6-6.9.0-py3-none-win_arm64.whl", hash = "sha256:1188f118d1c570d27fba39707e3d8a48525f979816e73de0da55b9e6fa9ad0a1", size = 49568913 },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pyqt6-sip"
|
||||
version = "13.10.0"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/90/18/0405c54acba0c8e276dd6f0601890e6e735198218d031a6646104870fe22/pyqt6_sip-13.10.0.tar.gz", hash = "sha256:d6daa95a0bd315d9ec523b549e0ce97455f61ded65d5eafecd83ed2aa4ae5350", size = 92464 }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/6b/0c/8d1de48b45b565a46bf4757341f13f9b1853a7d2e6b023700f0af2c213ab/PyQt6_sip-13.10.0-cp313-cp313-macosx_10_13_universal2.whl", hash = "sha256:7b6e250c2e7c14702a623f2cc1479d7fb8db2b6eee9697cac10d06fe79c281bb", size = 112343 },
|
||||
{ url = "https://files.pythonhosted.org/packages/af/13/e2cc2b667a9f5d44c2d0e18fa6e1066fca3f4521dcb301f4b5374caeb33e/PyQt6_sip-13.10.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:0fcb30756568f8cd59290f9ef2ae5ee3e72ff9cdd61a6f80c9e3d3b95ae676be", size = 322527 },
|
||||
{ url = "https://files.pythonhosted.org/packages/20/1a/5c6fcae85edb65cf236c9dc6d23b279b5316e94cdca1abdee6d0a217ddbb/PyQt6_sip-13.10.0-cp313-cp313-manylinux_2_5_x86_64.manylinux1_x86_64.whl", hash = "sha256:757ac52c92b2ef0b56ecc7cd763b55a62d3c14271d7ea8d03315af85a70090ff", size = 303407 },
|
||||
{ url = "https://files.pythonhosted.org/packages/b9/db/6924ec985be7d746772806b96ab81d24263ef72f0249f0573a82adaed75e/PyQt6_sip-13.10.0-cp313-cp313-win_amd64.whl", hash = "sha256:571900c44a3e38738d696234d94fe2043972b9de0633505451c99e2922cb6a34", size = 53580 },
|
||||
{ url = "https://files.pythonhosted.org/packages/77/c3/9e44729b582ee7f1d45160e8c292723156889f3e38ce6574f88d5ab8fa02/PyQt6_sip-13.10.0-cp313-cp313-win_arm64.whl", hash = "sha256:39cba2cc71cf80a99b4dc8147b43508d4716e128f9fb99f5eb5860a37f082282", size = 45446 },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "python-dateutil"
|
||||
version = "2.9.0.post0"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user