Bell State
Create quantum entanglement between two qubits - the foundation of quantum computing.
Overviewβ
The Bell state is the simplest example of quantum entanglement. When two qubits are entangled, measuring one instantly determines the state of the other, regardless of distance.
This example demonstrates:
- Creating a maximally entangled state
- Understanding quantum correlations
- Verifying entanglement quality
The Circuitβ
βββββ βββ
q_0: β€ H ββββ βββ€Mββββ
ββββββββ΄ββββ₯ββββ
q_1: ββββββ€ X βββ«ββ€Mβ
βββββ β ββ₯β
c: 2/ββββββββββββ©βββ©β
0 1
The circuit creates the Bell state:
|Ξ¦βΊβ© = (1/β2)(|00β© + |11β©)
Codeβ
"""
Bell State - Quantum Entanglement
=================================
Creates the maximally entangled Bell state |Ξ¦+β©.
Expected result: 50% |00β© and 50% |11β©, never |01β© or |10β©
"""
from softqcos_sdk import QCOSClient
def create_bell_state():
"""Create and execute a Bell state circuit."""
client = QCOSClient()
# Bell state circuit
circuit = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[2];
creg c[2];
// Create Bell state |Ξ¦+β©
h q[0]; // Put qubit 0 in superposition
cx q[0], q[1]; // Entangle with qubit 1
measure q -> c;
"""
print("π Bell State - Quantum Entanglement")
print("=" * 50)
# Execute
result = client.execute(qasm=circuit, shots=2048)
# Analyze results
print(f"\nπ Results (2048 shots):")
print("-" * 30)
expected_states = {"00", "11"}
unexpected_states = {"01", "10"}
total_expected = 0
total_unexpected = 0
for state in ["00", "01", "10", "11"]:
count = result.counts.get(state, 0)
percentage = (count / 2048) * 100
bar = "β" * int(percentage / 2)
if state in expected_states:
total_expected += count
marker = "β"
else:
total_unexpected += count
marker = "β" if count > 0 else " "
print(f" |{state}β©: {count:4d} ({percentage:5.1f}%) {bar} {marker}")
# Quality metrics
print(f"\nπ Entanglement Quality:")
# Correlation
if total_unexpected == 0:
correlation = 1.0
print(f" Perfect correlation: 100%")
else:
correlation = total_expected / 2048
print(f" Correlation: {correlation:.1%}")
# Balance between |00β© and |11β©
count_00 = result.counts.get("00", 0)
count_11 = result.counts.get("11", 0)
if count_00 > 0 and count_11 > 0:
balance = min(count_00, count_11) / max(count_00, count_11)
print(f" State balance: {balance:.1%}")
# Fidelity estimate
fidelity = (total_expected / 2048) * (balance if count_00 > 0 and count_11 > 0 else 1.0)
print(f" Estimated fidelity: {fidelity:.1%}")
if fidelity > 0.95:
print("\nπ Excellent entanglement quality!")
elif fidelity > 0.85:
print("\nβ
Good entanglement quality")
else:
print("\nβ οΈ Entanglement quality could be improved")
return result
def main():
return create_bell_state()
if __name__ == "__main__":
main()
Expected Outputβ
π Bell State - Quantum Entanglement
==================================================
π Results (2048 shots):
------------------------------
|00β©: 1031 ( 50.3%) βββββββββββββββββββββββββ β
|01β©: 0 ( 0.0%)
|10β©: 0 ( 0.0%)
|11β©: 1017 ( 49.7%) ββββββββββββββββββββββββ β
π Entanglement Quality:
Perfect correlation: 100%
State balance: 98.6%
Estimated fidelity: 98.6%
π Excellent entanglement quality!
All Four Bell Statesβ
There are four maximally entangled Bell states:
"""All Four Bell States"""
from softqcos_sdk import QCOSClient
def create_all_bell_states():
client = QCOSClient()
bell_states = {
"Ξ¦+": {
"circuit": """
OPENQASM 2.0; include "qelib1.inc";
qreg q[2]; creg c[2];
h q[0]; cx q[0], q[1];
measure q -> c;
""",
"expected": "|00β© + |11β©"
},
"Ξ¦-": {
"circuit": """
OPENQASM 2.0; include "qelib1.inc";
qreg q[2]; creg c[2];
x q[0]; h q[0]; cx q[0], q[1];
measure q -> c;
""",
"expected": "|00β© - |11β©"
},
"Ξ¨+": {
"circuit": """
OPENQASM 2.0; include "qelib1.inc";
qreg q[2]; creg c[2];
h q[0]; cx q[0], q[1]; x q[1];
measure q -> c;
""",
"expected": "|01β© + |10β©"
},
"Ξ¨-": {
"circuit": """
OPENQASM 2.0; include "qelib1.inc";
qreg q[2]; creg c[2];
x q[0]; h q[0]; cx q[0], q[1]; x q[1];
measure q -> c;
""",
"expected": "|01β© - |10β©"
}
}
print("π All Four Bell States")
print("=" * 60)
for name, state in bell_states.items():
result = client.execute(qasm=state["circuit"], shots=1024)
print(f"\n|{name}β© = (1/β2)({state['expected']})")
print(f" Counts: {dict(sorted(result.counts.items()))}")
if __name__ == "__main__":
create_all_bell_states()
Understanding Entanglementβ
Why Only |00β© and |11β©?β
The CNOT gate flips the target qubit (q[1]) only when the control qubit (q[0]) is |1β©:
- After H gate: q[0] is in superposition (|0β© + |1β©)/β2
- CNOT correlates q[1] with q[0]:
- When q[0] = |0β©: q[1] stays |0β© β |00β©
- When q[0] = |1β©: q[1] flips to |1β© β |11β©
The qubits are now entangled - they share a quantum correlation that cannot be described by classical physics.
Spooky Action at a Distanceβ
If you measure qubit 0 and get |0β©, you instantly know qubit 1 is also |0β©, even if the qubits are light-years apart. This is what Einstein called "spooky action at a distance."
Note: This doesn't allow faster-than-light communication because the measurement outcome is random.
Production Usageβ
With Optimizationβ
from softqcos_sdk import QCOSClient
client = QCOSClient()
# Optimize circuit for target hardware
optimized = client.optimize(
circuit=bell_circuit,
target="ionq",
objective="max_fidelity"
)
print(f"Gate reduction: {optimized.improvements['gate_reduction']}")
print(f"Optimized circuit:\n{optimized.optimized_qasm}")
# Execute optimized circuit
result = client.execute(qasm=optimized.optimized_qasm, shots=1024)
With Cost Estimationβ
# Estimate cost before execution
estimate = client.estimate_cost(
circuit=bell_circuit,
backend="azure_quantum_ionq",
shots=1024
)
print(f"Estimated cost: β¬{estimate.cost_eur:.4f}")
print(f"Estimated time: {estimate.time_seconds}s")
# Execute only if within budget
if estimate.cost_eur < 1.0:
result = client.execute(
qasm=bell_circuit,
backend="azure_quantum_ionq",
shots=1024
)
Next Stepsβ
- GHZ State - Scale entanglement to many qubits
- Quantum Teleportation - Use entanglement to transfer quantum states
- VQE Chemistry - Apply entanglement to real problems