Denmark - Flag Denmark

Incoterms:DDP
All prices include duty and customs fees on select shipping methods.

Please confirm your currency selection:

Danish Kroner
Free shipping on most orders over 330 kr. (DKK)

Euros
Free shipping on most orders over 50 € (EUR)

US Dollars
Free shipping on most orders over $60 (USD)

Bench Talk for Design Engineers

Bench Talk

rss

Bench Talk for Design Engineers | The Official Blog of Mouser Electronics


Quantum Programming Mike Parks

(Source: Happy Photo Stock/stock.adobe.com; generated with AI)

Since the 20th century, computing has been dominated by electrons, bits, and transistors, which are key building blocks of digital technology. While technology has shrunk and programming languages have come and gone, the fundamentals have stayed the same for the most part, with the electron ruling the day. I recall cutting my embedded engineering teeth with Stamp microcontrollers and the PBASIC programming language. It was cumbersome at best. Flash forward 20 years, devices such as Arduino and languages like CircuitPython have significantly improved the ease of programming embedded systems. But still, the electron and the bit represent the fundamentals, just as they did in my Stamp PBASIC days.

However, all that might change tomorrow. A paradigm shift is emerging. Quantum computing is the new kid in town, and it’s changing the tenets of computing in the 21st century. To help understand these changes, this blog outlines how quantum programming languages move beyond classical bits to harness superposition, entanglement, and interference, shaping the future of computation.

What is Quantum Programming?

At first glance, the syntax of quantum programming languages looks only slightly different from the current languages used in traditional computing, such as Python, Java, C++, Rust, or Go. For example, a code snippet written in Google’s Cirq programming language looks like this:

 

import cirq

 

# Define two qubits

q0, q1 = cirq.LineQubit.range(2)

 

# Create a quantum circuit

circuit = cirq.Circuit(

    cirq.H(q0),  # Hadamard gate

    cirq.CX(q0, q1),  # CNOT gate

    cirq.measure(q0, q1)  # Measurement

)

 

print(circuit)

 

We can already see some familiar programming concepts without trying to understand what the Cirq code does. Functions, variables, and assignment operators are similar to Python, so it’s probably safe to assume that Google based Cirq on that popular language. Other quantum programming languages, such as Microsoft’s Q#, IBM’s Qiskit, and Quipper, all have a familiar syntactical look and feel.

So, what is the difference between quantum programming and traditional programming? Let’s take a step back and understand the fundamental nature of quantum computing.

The Basics of Quantum

Quantum computing is a rapidly evolving ecosystem. It is far from mature and will likely undergo significant growth and radical changes in the months and years ahead. But for now, let’s look at some fundamental topics that are key to understanding quantum programming. And don’t worry; you are not alone if it all feels strange and overwhelming.

Let’s begin by imagining a light switch. In a traditional computer, a bit is like that switch—it can be either ON (1) or OFF (0). That's it. Everything is based on these definite states.

Now, imagine a dimmer switch. It's not just ON or OFF; it can be anywhere in between. That's what a qubit is like. The qubit is the fundamental unit of information in a quantum computer.

Here's where it gets mind-bending:

  • Superposition: A qubit can be in a superposition, meaning it can be 0 and 1 simultaneously. Think of it as the dimmer switch being at multiple brightness levels simultaneously. It's not that we don't know which state it's in; it's genuinely in both states at once. This allows a quantum computer to process multiple possibilities at once.
  • Entanglement: This is where qubits get spooky. Imagine two qubits that are linked. If you change the state of one, the other instantly changes, no matter how far apart they are. It's like having two dimmer switches that are always perfectly synced.
  • Interference: Quantum computers use probability waves that can constructively or destructively interfere to enhance correct answers and cancel out incorrect ones.
  • Decoherence & Noise: Qubits are sensitive to external disturbances such as heat and electromagnetic radiation, which can disturb superpositioning and lead to calculation errors.
  • Probabilistic: Like neural networks associated with machine learning, quantum computing’s output is probabilistic. Each potential output is assigned a percentage representing the likelihood of its correctness.
  • Quantum Gates: Whereas classical gates have bits that are definite 0 or 1 and operations are based on logic gates (AND, OR, NOT), qubits can be in superposition and entangled, and defined states are |0⟩ or |1⟩. Operations are based on quantum gates (Hadamard, CNOT).

What Do Quantum Programming Languages Actually Do?

Just like we have programming languages for classical computers, we need languages to tell quantum computers what to do. Quantum programming languages are designed to:

  • manage qubits and their states,
  • implement quantum gates and circuits, and
  • handle the unique challenges of quantum computing, like decoherence.

In contrast to classical bits, qubits can simultaneously exist in multiple states because of superposition and entanglement. Properly managing these states is crucial for accurate quantum computation. Quantum gates are the mechanism by which qubits are manipulated. To achieve superposition, a Hadamard gate (H) is applied to a qubit. Additionally, applying a CNOT gate to two qubits can entangle them. Other types of quantum gates include:

  • Pauli-X (X): Bit flip (NOT gate)
  • Pauli-Y (Y): Like X but with a phase shift
  • Pauli-Z (Z): Phase flip
  • Toffoli (CCX): Classical AND logic in quantum

Once quantum gates manipulate qubits, gates are linked together into quantum circuits. Different quantum circuit topologies encode a variety of quantum algorithms. These algorithms solve specific computationally intensive problems where exploiting the quantum phenomena of entanglement and superposition help speed up computations. These problems involve cryptography, optimization, systems of linear equations, and scientific questions involving climate, medicine, and high-energy particles, to name a few. In short, quantum programming will benefit some but not all computational problems.

Two of the more famous quantum algorithms are:

  • Shor's Algorithm: This algorithm can factor large numbers exponentially faster than the best-known classical algorithms. This capability has huge implications for cryptography, as many encryption methods hinge on the difficulty of factoring large numbers.
     
  • Grover's Algorithm: This algorithm can search an unsorted database much faster than classical algorithms. Think of it like finding a specific name in a giant phone book that lacks any order.

It’s important to note that when you run a quantum program, you get probabilistic results, so best practices call for running the program multiple times to get a statistically significant answer.

Putting It All Together

So, with all this newfound information under our belt, let’s go back to the Cirq code snippet from before and try to understand what exactly is going on.

  1. Import Cirq: This imports the Cirq library, which is a Python framework for designing and simulating quantum circuits.
     

import cirq
 

  1. Define Two Qubits: This creates two qubits, q0 and q1, and places them on a 1D line (hence, LineQubit). The range(2) function assigns q0 to index 0 and q1 to index 1.
     

q0, q1 = cirq.LineQubit.range(2)
 

  1. Construct the Quantum Circuit

circuit = cirq.Circuit(

    cirq.H(q0), 

    cirq.CX(q0, q1),

    cirq.measure(q0, q1) 

)

  • cirq.H(q0): Places q0 into superposition, meaning it has an equal chance of being |0⟩ or |1⟩. This means q0 is now in a 50/50 probability of 0 or 1.
  • cirq.CX(q0, q1): Creates quantum entanglement between q0 and q1. The CNOT (Controlled-NOT) gate flips q1 only if q0 is |1⟩. This means the two qubits are now entangled—if one is measured as 0, the other must also be 0; if one is 1, the other must be 1.
  • cirq.measure(q0, q1): Measures both q0 and q1, collapsing their quantum states into either |00⟩ or |11⟩ with an equal probability of 50 percent.
     
  1. Print the Circuit

print(circuit)

This outputs a text-based visualization of the quantum circuit:

0: ───H───@───M───

          │

1: ───────X───M───

 

So, in a nutshell, this Cirq circuit does the following:

  1. Creates two qubits (q0, q1)
  2. Applies a Hadamard gate to q0 (superposition)
  3. Uses a CNOT gate to entangle q0 and q1
  4. Measures both qubits, collapsing them into |00⟩ or |11⟩ with equal probability. The expected outputs of the circuit are as follows:
  • 50 percent of the time, the measurement result is "00"
  • 50 percent of the time, the measurement result is "11"
  • You never get "01" or "10" because the qubits are entangled.

Lastly, we can add some code to load the quantum circuit into a simulator, run it 1,000 times, and then plot the output as a histogram. The final code will look as follows:
 

import cirq

import matplotlib.pyplot as plt

 

# Define two qubits

q0, q1 = cirq.LineQubit.range(2)

 

# Create a quantum circuit

circuit = cirq.Circuit(

    cirq.H(q0),  # Hadamard gate to put q0 in superposition

    cirq.CX(q0, q1),  # CNOT gate to entangle q0 and q1

    cirq.measure(q0, q1)  # Measure both qubits

)

 

# Print the quantum circuit

print("Quantum Circuit:")

print(circuit)

 

# Initialize a quantum simulator

simulator = cirq.Simulator()

 

# Run the circuit multiple times to gather statistics

result = simulator.run(circuit, repetitions=1000)

 

# Get measurement results as a histogram

histogram = result.histogram(key="0,1")

 

# Convert histogram data into a readable format

keys = [bin(k)[2:].zfill(2) for k in histogram.keys()]  # Convert integer keys to binary (00, 11)

values = [histogram[k] for k in histogram.keys()]  # Get counts

 

# Print raw measurement counts

print("\nMeasurement Results:", dict(zip(keys, values)))

 

# Create a histogram plot

plt.bar(keys, values, color='royalblue')

plt.xlabel("Measurement Outcome")

plt.ylabel("Frequency")

plt.title("Quantum Circuit Measurement Results")

plt.show()

 

Conclusion

Quantum programming is fundamentally different from classical programming—not just in how we write code, but in how computation itself works. While quantum programming languages like Cirq, Qiskit, and Q# may appear syntactically familiar, the underlying mechanics—superposition, entanglement, and interference—introduce an entirely new way of thinking about computation.

Unlike classical bits that operate within binary constraints, qubits exist in fluid, probabilistic states, requiring new techniques to manage their behavior and extract useful results. Quantum circuits, rather than executing sequential logic, manipulate qubits using quantum gates, constructing algorithms that employ quantum mechanics to solve problems that surpass the reach of classical systems.

While still in its early stages, quantum computing holds transformational potential. It promises exponential acceleration in cryptography, drug discovery, materials science, optimization, and AI. However, it’s important to recognize that quantum computing is not a universal replacement for classical methods. Instead, it is a powerful complement, best suited for problems where quantum effects provide a computational advantage.

As quantum hardware advances and quantum programming becomes more accessible, the next generation of developers will need to rethink what it means to write software. The era of electrons, bits, and transistors is now sharing the stage with qubits, quantum gates, and probabilistic computing. Whether as researchers, engineers, or programmers, those who understand quantum computing today are helping shape the future of computation itself.



« Back


Michael Parks, P.E. is the co-founder of Green Shoe Garage, a custom electronics design studio and embedded security research firm located in Western Maryland. He produces the Gears of Resistance Podcast to help raise public awareness of technical and scientific matters. Michael is also a licensed Professional Engineer in the state of Maryland and holds a Master’s degree in systems engineering from Johns Hopkins University.


All Authors

Show More Show More
View Blogs by Date

Archives