Hello Quantum
Your first quantum circuit with SoftQCOS - a simple superposition state.
Overviewβ
This example demonstrates:
- Creating a quantum circuit
- Submitting to SoftQCOS API
- Retrieving and interpreting results
The Circuitβ
We'll create the simplest interesting quantum circuit: a single qubit in superposition.
ββββββββ
q: β€ H ββ€Mβ
βββββββ₯β
c: 1/βββββββ©β
0
The Hadamard gate (H) puts the qubit in an equal superposition of |0β© and |1β©.
Codeβ
"""
Hello Quantum - Your First SoftQCOS Circuit
============================================
This example creates a superposition state and measures it.
Expected result: ~50% |0β© and ~50% |1β©
"""
from softqcos_sdk import QCOSClient
def main():
# Initialize client
client = QCOSClient()
# Define circuit in OpenQASM 2.0
circuit = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[1];
creg c[1];
h q[0]; // Apply Hadamard gate
measure q -> c; // Measure qubit
"""
print("π¬ Hello Quantum World!")
print("=" * 50)
print("\nπ Circuit: Single qubit superposition")
print(" Gate: Hadamard (H)")
print(" Expected: 50% |0β©, 50% |1β©")
# Execute circuit
print("\nβ³ Submitting circuit...")
result = client.execute(qasm=circuit, shots=1024)
# Display results
print("\nβ
Results:")
print(f" Job ID: {result.job_id}")
print(f" Status: {result.status}")
print(f"\nπ Measurement counts:")
total = sum(result.counts.values())
for state, count in sorted(result.counts.items()):
percentage = (count / total) * 100
bar = "β" * int(percentage / 2)
print(f" |{state}β©: {count:4d} ({percentage:5.1f}%) {bar}")
# Verify superposition quality
if len(result.counts) == 2:
values = list(result.counts.values())
ratio = min(values) / max(values)
if ratio > 0.9:
print("\nπ Perfect superposition! Ratio: {:.2%}".format(ratio))
else:
print(f"\nβ οΈ Slight imbalance. Ratio: {ratio:.2%}")
return result
if __name__ == "__main__":
result = main()
Expected Outputβ
π¬ Hello Quantum World!
==================================================
π Circuit: Single qubit superposition
Gate: Hadamard (H)
Expected: 50% |0β©, 50% |1β©
β³ Submitting circuit...
β
Results:
Job ID: job_a1b2c3d4e5f6
Status: completed
π Measurement counts:
|0β©: 518 ( 50.6%) βββββββββββββββββββββββββ
|1β©: 506 ( 49.4%) ββββββββββββββββββββββββ
π Perfect superposition! Ratio: 97.68%
Understanding the Resultsβ
What is Superposition?β
When we apply the Hadamard gate to a qubit starting in |0β©:
H|0β© = (1/β2)(|0β© + |1β©)
The qubit is now in a superposition - it's both |0β© and |1β© simultaneously with equal probability.
Why ~50/50?β
When we measure a qubit in superposition, the wavefunction "collapses" to one of the basis states:
- 50% probability of measuring |0β©
- 50% probability of measuring |1β©
With 1024 shots, we expect approximately 512 of each outcome.
Statistical Variationβ
The exact counts will vary due to quantum randomness. This is not a bug - it's the fundamental nature of quantum mechanics!
Variationsβ
Using Qiskit to Build the Circuitβ
from qiskit import QuantumCircuit
from softqcos_sdk import QCOSClient
# Build circuit with Qiskit
qc = QuantumCircuit(1, 1)
qc.h(0)
qc.measure(0, 0)
# Convert to QASM and execute
client = QCOSClient()
result = client.execute(qasm=qc.qasm(), shots=1024)
print(result.counts)
Multiple Qubits in Superpositionβ
circuit = """
OPENQASM 2.0;
include "qelib1.inc";
qreg q[3];
creg c[3];
h q[0];
h q[1];
h q[2];
measure q -> c;
"""
# Expected: Equal distribution across all 8 states (000, 001, ..., 111)
result = client.execute(qasm=circuit, shots=4096)
Next Stepsβ
- Bell State - Create entanglement between qubits
- GHZ State - Scale to many qubits