Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Last active August 6, 2025 21:02
Show Gist options
  • Save primaryobjects/235fd81d08bf26f06b66d421053144f0 to your computer and use it in GitHub Desktop.
Save primaryobjects/235fd81d08bf26f06b66d421053144f0 to your computer and use it in GitHub Desktop.
Quantum entanglement to display two different characters using Qiskit and Python. Quantum computing.
# Imports
!pip install qiskit
!pip install qiskit-aer
!pip install pylatexenc
# Quantum program
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, transpile
from qiskit_aer import Aer
from qiskit.visualization import plot_histogram
import matplotlib.pyplot as plt
from numpy import pi
qreg_q = QuantumRegister(4, 'q')
creg_c = ClassicalRegister(4, 'c')
qc = QuantumCircuit(qreg_q, creg_c)
qc.h(qreg_q[0])
qc.cx(qreg_q[0], qreg_q[1])
qc.barrier(qreg_q)
qc.measure(qreg_q, creg_c)
qc.draw(output='mpl')
# Running the program
numbers = []
results = {}
# Select the simulator.
simulator = Aer.get_backend('aer_simulator')
compiled_circuit = transpile(qc, simulator)
for i in range(100):
# Execute the circuit.
job = simulator.run(compiled_circuit)
result = job.result()
counts = result.get_counts()
# Find the most frequent hit count.
key = max(counts, key=counts.get)
# Since the quantum computer returns a binary string (one bit for each qubit), we need to convert it to an integer.
num = int(key, 2)
numbers.append(num)
# Count occurrences of each value for plotting a histogram of the random values.
results[num] = results[num] + 1 if num in results else 1
# Print character based on last 4 bits
if num == (ord('@') & 0xF): # Compare to last 4 bits of '@' (64)
print(f"Result: {key} -> Character: '@'")
elif num == (ord('C') & 0xF): # Compare to last 4 bits of 'C' (67)
print(f"Result: {key} -> Character: 'C'")
else:
print(f"Result: {key} -> No matching character")
print(counts)
plot_histogram(counts)
from matplotlib.textpath import TextPath
from matplotlib.patches import PathPatch
from matplotlib.font_manager import FontProperties
# Font and figure setup
fig, ax = plt.subplots(figsize=(6, 6)) # Bigger canvas
ax.set_aspect('equal')
ax.axis('off')
fp = FontProperties(family="DejaVu Sans", weight="bold")
size = 200 # Large font
# ASCII and bit values
at_code = ord('@') # 64
c_code = ord('C') # 67
at_4bit = at_code & 0xF # 0
c_4bit = c_code & 0xF # 3
# Total occurrences
total_shots = sum(results.values())
at_count = results.get(at_4bit, 0)
c_count = results.get(c_4bit, 0)
# Calculate opacity
alpha_at = at_count / total_shots
alpha_c = c_count / total_shots
# Draw superimposed characters
x, y = 0.0, 0.0
if at_count > 0:
path_at = TextPath((x, y), "@", size=size, prop=fp)
patch_at = PathPatch(path_at, facecolor='blue', alpha=alpha_at, label=f"'@' ({at_count})")
ax.add_patch(patch_at)
if c_count > 0:
path_c = TextPath((x, y), "C", size=size, prop=fp)
patch_c = PathPatch(path_c, facecolor='red', alpha=alpha_c, label=f"'C' ({c_count})")
ax.add_patch(patch_c)
# Expand limits to prevent clipping
ax.set_xlim(-50, 250)
ax.set_ylim(-50, 250)
# Show result
ax.legend(loc='upper right')
plt.title("Superimposed '@' and 'C' — Opacity ∝ Frequency")
plt.show()
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@primaryobjects
Copy link
Author

Run the Python Notebook online here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment