Initial monorepo — everything BlackRoad in one place
bin/ 230 CLI tools (ask-*, br-*, agent-*, roadid, carpool) scripts/ 99 automation scripts fleet/ Node configs and deployment workers/ Cloudflare Worker sources (roadpay, road-search, squad webhooks) roadc/ RoadC programming language roadnet/ Mesh network (5 APs, WireGuard) operator/ Memory system scripts config/ System configs dotfiles/ Shell configs docs/ Documentation BlackRoad OS — Pave Tomorrow. RoadChain-SHA2048: d1a24f55318d338b RoadChain-Identity: alexa@sovereign RoadChain-Full: d1a24f55318d338b24b60bad7be39286379c76ae5470817482100cb0ddbbcb97e147d07ac7243da0a9f0363e4e5c833d612b9c0df3a3cd20802465420278ef74875a5b77f55af6fe42a931b8b635b3d0d0b6bde9abf33dc42eea52bc03c951406d8cbe49f1a3d29b26a94dade05e9477f34a7d4d4c6ec4005c3c2ac54e73a68440c512c8e83fd9b1fe234750b898ef8f4032c23db173961fe225e67a0432b5293a9714f76c5c57ed5fdf35b9fb40fd73c03ebf88b7253c6a0575f5afb6a6b49b3bda310602fb1ef676859962dad2aebbb2875814b30eee0a8ba195e482d4cbc91d8819e7f38f6db53e8063401649c77bb994371473cabfb917fb53e8cbe73d60
This commit is contained in:
179
roadc/examples/quantum_hello.road
Normal file
179
roadc/examples/quantum_hello.road
Normal file
@@ -0,0 +1,179 @@
|
||||
# Quantum Hello World - BlackRoad's First Quantum Program!
|
||||
# Creates a Bell state and demonstrates quantum entanglement
|
||||
|
||||
## Create Bell State (Entangled Qubits)
|
||||
fun create_bell_state():
|
||||
print("🌌 Creating Bell State (Quantum Entanglement)...")
|
||||
|
||||
# Initialize two qubits in |0⟩ state
|
||||
let q1: qubit = |0⟩
|
||||
let q2: qubit = |0⟩
|
||||
|
||||
print("Initial state: |00⟩")
|
||||
|
||||
# Apply Hadamard to first qubit (creates superposition)
|
||||
H(q1)
|
||||
print("After Hadamard: (|00⟩ + |10⟩) / √2")
|
||||
|
||||
# Apply CNOT (creates entanglement)
|
||||
CNOT(q1, q2)
|
||||
print("After CNOT: (|00⟩ + |11⟩) / √2 ← ENTANGLED! ✨")
|
||||
|
||||
# Get probabilities
|
||||
let probs = probabilities([q1, q2])
|
||||
print("\nProbabilities:")
|
||||
print(" |00⟩: {probs[0]}") # 50%
|
||||
print(" |11⟩: {probs[3]}") # 50%
|
||||
|
||||
# Measure
|
||||
let result = measure([q1, q2])
|
||||
print("\nMeasurement: |{result[0]}{result[1]}⟩")
|
||||
print("(Both qubits always match due to entanglement!)")
|
||||
|
||||
## Quantum Superposition Demo
|
||||
fun superposition_demo():
|
||||
print("\n🎲 Quantum Superposition Demo...")
|
||||
|
||||
let q: qubit = |0⟩
|
||||
|
||||
# Create superposition
|
||||
H(q)
|
||||
print("State: |+⟩ = (|0⟩ + |1⟩) / √2")
|
||||
|
||||
# Show probabilities
|
||||
let probs = probabilities(q)
|
||||
print("Probability of |0⟩: {probs[0]}") # 50%
|
||||
print("Probability of |1⟩: {probs[1]}") # 50%
|
||||
|
||||
# Measure many times
|
||||
print("\nMeasuring 10 times:")
|
||||
for i in 0..10:
|
||||
let q_fresh: qubit = |0⟩
|
||||
H(q_fresh)
|
||||
let result = measure(q_fresh)
|
||||
print(" Shot {i+1}: {result}")
|
||||
|
||||
## Qutrit Demo (3-dimensional quantum state)
|
||||
fun qutrit_demo():
|
||||
print("\n🎯 Qutrit Demo (3-level quantum system)...")
|
||||
|
||||
let qt: qutrit = |0⟩
|
||||
print("Initial state: |0⟩")
|
||||
|
||||
# Create equal superposition over 3 states
|
||||
H3(qt)
|
||||
print("After H3: (|0⟩ + |1⟩ + |2⟩) / √3")
|
||||
|
||||
# Get probabilities
|
||||
let probs = probabilities(qt)
|
||||
print("\nProbabilities:")
|
||||
print(" |0⟩: {probs[0]}") # 33.3%
|
||||
print(" |1⟩: {probs[1]}") # 33.3%
|
||||
print(" |2⟩: {probs[2]}") # 33.3%
|
||||
|
||||
# Measure
|
||||
let result = measure(qt)
|
||||
print("\nMeasurement: |{result}⟩")
|
||||
print("(Could be 0, 1, or 2!)")
|
||||
|
||||
## Quantum Teleportation
|
||||
fun quantum_teleportation():
|
||||
print("\n📡 Quantum Teleportation Demo...")
|
||||
|
||||
# Alice has a qubit in unknown state
|
||||
let alice_qubit: qubit = |0⟩
|
||||
RY(alice_qubit, 0.7) # Create arbitrary state
|
||||
print("Alice's qubit: in superposition state")
|
||||
|
||||
# Create entangled pair (Alice and Bob)
|
||||
let alice_epr: qubit = |0⟩
|
||||
let bob_epr: qubit = |0⟩
|
||||
H(alice_epr)
|
||||
CNOT(alice_epr, bob_epr)
|
||||
print("Entangled pair created between Alice and Bob")
|
||||
|
||||
# Alice performs Bell measurement
|
||||
CNOT(alice_qubit, alice_epr)
|
||||
H(alice_qubit)
|
||||
let m1 = measure(alice_qubit)
|
||||
let m2 = measure(alice_epr)
|
||||
print("Alice measures: {m1}, {m2}")
|
||||
print("(Alice's qubit is now destroyed)")
|
||||
|
||||
# Bob applies corrections based on Alice's measurements
|
||||
if m2 == 1:
|
||||
X(bob_epr)
|
||||
if m1 == 1:
|
||||
Z(bob_epr)
|
||||
|
||||
print("Bob applies corrections")
|
||||
print("✨ Teleportation complete! Bob now has the exact state! ✨")
|
||||
|
||||
## Grover's Search
|
||||
fun grovers_search():
|
||||
print("\n🔍 Grover's Algorithm (Quantum Search)...")
|
||||
|
||||
# Search for element "3" in list of 8 elements
|
||||
let n_qubits = 3 # 2^3 = 8 elements
|
||||
let target = 3
|
||||
|
||||
print("Searching for element {target} in 8-element list")
|
||||
print("Classical search: O(N) = 8 operations")
|
||||
print("Quantum search: O(√N) = ~3 operations")
|
||||
|
||||
# Initialize register
|
||||
let register: qreg[3] = |000⟩
|
||||
|
||||
# Create superposition
|
||||
for q in register:
|
||||
H(q)
|
||||
|
||||
# Grover iterations (√N ≈ 2.8, so 3 iterations)
|
||||
for iter in 0..3:
|
||||
# Oracle (marks target state)
|
||||
if target == 3: # |011⟩
|
||||
Z(register[0])
|
||||
Z(register[1])
|
||||
|
||||
# Diffusion operator
|
||||
for q in register:
|
||||
H(q)
|
||||
for q in register:
|
||||
X(q)
|
||||
MCZ(register[0..2], register[2])
|
||||
for q in register:
|
||||
X(q)
|
||||
for q in register:
|
||||
H(q)
|
||||
|
||||
# Measure
|
||||
let result = measure(register)
|
||||
print("\nFound: {result}")
|
||||
print("✨ Quantum speedup achieved! ✨")
|
||||
|
||||
## Main Program
|
||||
fun main():
|
||||
print("╔════════════════════════════════════════════╗")
|
||||
print("║ 🌌 BlackRoad Quantum Computing Demo 🌌 ║")
|
||||
print("╚════════════════════════════════════════════╝")
|
||||
print("")
|
||||
|
||||
# Configure quantum backend
|
||||
quantum.backend = "simulator"
|
||||
quantum.shots = 1024
|
||||
|
||||
# Run demos
|
||||
create_bell_state()
|
||||
superposition_demo()
|
||||
qutrit_demo()
|
||||
quantum_teleportation()
|
||||
grovers_search()
|
||||
|
||||
print("\n╔════════════════════════════════════════════╗")
|
||||
print("║ ✨ Quantum Computing on BlackRoad! ✨ ║")
|
||||
print("╚════════════════════════════════════════════╝")
|
||||
print("")
|
||||
print("Next: Try quantum_ml.road for machine learning!")
|
||||
print("Or: quantum_chemistry.road for molecular simulation!")
|
||||
print("")
|
||||
print("🖤🛣️ BlackRoad OS Language - Classical meets Quantum!")
|
||||
Reference in New Issue
Block a user