First experiment using the Hadamard gate

First experiment using the Hadamard gate
Photo by jonakoh _ / Unsplash

I created my first notebook using the Hadamard gate in qiskit. Notebook is located here.

The basic explanation of the Hadamard gate is that it is a single qubit quantum gate that puts a qubit in superposition. The gate takes the qubit and gives it an equal probability of ending up either 1 or 0. It’s also usually just written as H.

Brief summary

There are a lot of different ways that quantum popularizers have attempted to describe superposition. The worst description is that you have two states, 0 or 1 (on and off, yes or no) and that superposition is both yes and no. The best description I’ve seen is that you have two states, 0 or 1, and that superposition is a state that has not yet settled to 1 or 0.

Superposition sits in the wave function and an observation will cause superposition to collapse into 0 or 1. The probability of the state it will collapse into is the squared magnitude of its amplitude. An amplitude is a complex number, a combination of a real number and an imaginary number. To get a probability, you multiply the amplitude by its complex conjugate (the same number with the imaginary part’s sign flipped). The imaginary parts cancel and you’re left with a real number. The probabilities of every option add up to 1.0.

Brass tacks

You probably noticed this line in the ipynb:

H = (1/np.sqrt(2)) * np.array([[1, 1],
[1, -1]], dtype=complex)

The core of the gate is the 1/√2. Square it and you get 1/2: an equal chance of 0 or 1. Use it as a scalar against the matrix (this one is called a Hadamard matrix) and you get this:

You'd expect a matrix of .5 all around, right, and not .707. But those entries are amplitudes, not probabilities. Square .707 to convert:

.707 * .707 =0.5

This diagram shows the Hadamard gate taking q (the qubit), which starts at |0⟩ by default, and putting it into an equal superposition of |0⟩ and |1⟩.

The M means measure. Measuring is the same as observing: it collapses the qubit to a definite state.

Put it all together: take the qubit, pass it through the Hadamard gate so it holds a probability instead of a definite state, then collapse it to 0 or 1.

Running multiple shots

When you do this several times, whether 10, 100, 1,000, or 10,000 times, you get a string of bits.

Keep in mind that qiskit running on a local machine is just a model of how a quantum machine operates. You’re basically just emulating the behavior of a quantum computer on a traditional terminal. In this instance, qiskit is a psuedo-random emulation of the behavior of a quantum computer.

Settling to .50 and .50

Flip a fair coin enough times and the results settle toward .50 heads and .50 tails. Any finite run will drift a little, and that drift shrinks relative to the total as the number of flips grows.

Running 1,000 shots through the Hadamard gate gave me:

Is .517 versus .483 in line with what you’d expect from 1,000 shots? That’s 17 more 0s than a perfect split.

Test it with the standard deviation for a count:

sqrt(.5 * .5 / 1000) * 1000

Meaning, the standard error of a 50/50 split over 1,000 shots, multiplied by 1,000 to put it back in counts.

The result is:

sqrt((.5 * .5) / 1000) * 1000 =15.811

So 17 off over 1,000 shots is right in line with expectations. Over the course of 10,000 shots, the split tightens up significantly:

{'1': 5005, '0': 4995}

Don’t read too much into the count itself; it was a lucky run that it landed that way. Landing at 5,005:4,995 happens about 9% of the time.

In this scenario, a typical 1,000-shot run is off by about 1.6 percentage points, and a typical 10,000-shot run is off by about 0.5. More shots gets you closer, as you’d expect.

Michael Gonzalez

Michael Gonzalez

Active CDO with 10+ years leading data in high-regulation sectors. Focused on the SAGE stack (Snowflake/AWS/GenAI) and the shift from passive reporting to active data engineering.