Skip to main content

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​