## ARTICLE

# All about Hadamard Gates

*From **Quantum Computing for Developers** by Johan Vos*

This article discusses The Hadamard Gate: what it is, what it has to do with quantum computing, and how to create a simple quantum applications with a Hadamard Gate and some Java code.

__________________________________________________________________

Take 37% off *Quantum Computing for Developers** *by entering **fccvos** into the discount code box at checkout at manning.com.

__________________________________________________________________

I’m going to start with an explanation of the Hadamard Gate: if you are already familiar with it and want to get to the Java examples, skip to the second part of the article.

**The Hadamard Gate, the gate to superposition**

In order to bring a particle into a superposition state, some very high-skilled physics need to be applied. Fortunately, as a developer, bringing a qubit into a superposition state simply requires applying a specific gate to that qubit.

The Hadamard Gate is a well-known gate in quantum computing that achieves this. Similar to the Pauli-X gate, the Hadamard Gate acts on a single qubit, and can be represented by a 2 x 2 matrix as well.

The Hadamard Gate is defined as follows:

*Equation 1*

We want to find out what happens when we apply this gate on a qubit that is in the **|0>** state. This can be inspected by multiplying the gate matrix to the qubit vector:

*Equation 2*

This equation shows that, after applying the Hadamard Gate to a qubit that is in the **|0>** state, the qubit enters a new state where the probability of measuring 0 is:

*Equation 3*

And the probability of measuring 1 is also:

*Equation 4*

In conclusion, applying the Hadamard Gate to a qubit that is in state `|0>`

brings the qubit in a superposition state where the probability of measuring 0 is equal to the probability of measuring 1.

What would happen if we apply the Hadamard Gate to a qubit that is in state `|0>`

?

The vector representation of a qubit in that state is given by:

*Equation 5*

Hence, applying a Hadamard Gate to this qubit means multiplying the Hadamard matrix with the above vector:

*Equation 6*

If we would measure the qubit at this point, the chance of measuring 0 would be:

*Equation 7*

and the chance of measuring 1 would be:

*Equation 8*

Hence, in both cases (qubit |0> or qubit |1>) applying a Hadamard Gate gives an equal chance for the qubit to be `0`

or `1'`

when measured.

**The Hadamard Gate in action**

Now that we have a little background, let’s create a simple quantum application with a Hadamard Gate. The relevant code for this example is shown below:

**public static void** **singleExecution**(String[] args) {

QuantumExecutionEnvironment simulator = **new** SimpleQuantumExecutionEnvironment();

Program program = **new** Program(1);

Step step = **new** Step();

step.addGate(**new** Hadamard(0));

program.addStep(step);

Result result = simulator.runProgram(program);

Qubit[] qubits = result.getQubits();

Qubit zero = qubits[0];

**int value** = zero.measure();

System.**out**.println("Value = "+**value**);

}

We create the `QuantumExecutionEnvironment`

which runs our program. Next we create a `Program`

instance which deals with a single qubit, and we create a `Step`

instance.

Instead of adding the Pauli-X gate to that step, we now add the Hadamard Gate to the step:

`step.addGate(`**new** Hadamard(0));

This applies a Hadamard Gate to the qubit. By default, qubits are originally in the |0> state. After applying a Hadamard Gate to a qubit which is in that state, there’s fifty percent chance the qubit, when measured is `zero`

and a fifty percent chance that it will be `one`

.

The remainder of the code snippet adds the step to the program, and runs the program on the simulator.

Finally, we measure the qubit, and print the value.

If you run this program once, you’ll either see:

`Value = 0`

or

`Value = 1`

After the sample code prints out the measured value, it visualizes the quantum circuit using StrangeFX. This is done using the following line of code:

`Renderer.renderProgram(program);`

As a result of this action, a window is shown containing the quantum circuit, as can be seen in Figure 2.

As can be seen from this picture, the resulting qubit has a 50% probability of being measured as *1*.

The second part of the sample invokes the `manyExecution`

function, which is very similar to the `singleExecution`

discussed above, but this time you run the program 1000 times. The `QuantumExecutionEnvironment`

and the Program have to be created only once. After the program has been created, the following loop is added to the application:

**Listing 1. Multiple runs of the Hadamard snippet**

`int cntZero = 0;`

int cntOne = 0;

for (int i = 0; i < 1000; i++) {A 1

Result result = simulator.runProgram(program); 2

Qubit[] qubits = result.getQubits();

Qubit zero = qubits[0];

int value = zero.measure(); 3

if (value == 0) cntZero++; 4

if (value == 1) cntOne++;

}

**1 You run the following loop 1000 times**

**2 You run the quantum program**

**3 You measure the qubit**

**4 Based on the measured valued (‘0’ or ‘1’) you increment one counter or the other.**

From this snippet, it can be seen that the `runProgram`

method is called 1000 times on the simulator. Each time, you measure the resulting qubit. If the qubit holds the value 0, the `cntZero`

counter is incremented. If the qubit holds the value 1, the `cntOne`

counter is incremented. After applying this loop, the results are printed:

`System.out.println("Applied Hadamard circuit 1000 times, got "`

+ cntZero + " times 0 and " + cntOne + " times 1.");

The result of this application therefore shows something similar to:

`==================================================`

1000 runs of a Quantum Circuit with Hadamard Gate

Applied Hadamard circuit 1000 times, got 510 times 0 and 490 times 1.

==================================================

What you created here, is a random number generator using the low-level Quantum API’s. The single qubit in the program is brought into a superposition, and then measured. When running this program on the Quantum Simulator, or by extension on any classical computer that *simulates* quantum behavior, the randomness is still somehow deterministic, as you use classic algorithms to generate a random number. Typically, simulators work with probability vectors, and when a measurement is required, a random number is used to pick one of the probabilities — taking into account, of course, the value of the probabilities.

On real quantum hardware, this is different. Nature itself will pick one value when we measure the qubit. This process is truly random (at least, this is what most quantum physicists currently assume). While this simple application seems a complex way to generate a random number, it has real value. It shows how one can generate a truly random number using quantum hardware. Random numbers are extremely important in a number of areas including encryption.

That’s all for now. If you want to learn more about the book, check it out on our browser-based liveBook reader here and see this slide deck.