# #showdev #react The Beginner's Quick and Dirty Guide to Quantum Computer Programming

### A Short Introduction...

We've been hearing a lot about quantum computing but as of yet, there isn't really an application that impacts our daily lives. The simple reason for this is because we don't have error-free quantum computers yet. This makes the time we're living in a sweet spot for developers wanting to jump into the field, because by the time they're out, it could be too late.
This is what drove the team I worked with to create a tool that allows users to jump in and start programming without having much knowledge about quantum computing. All you have to do is learn the basic logic gates, drag and drop, and click simulate to run your program. One of the things we focused on was design, we believe every good product is both beautiful and practical - it has form. (We're still working to make it better and would love your feedback).

Without further ado, here's a beginners guide to quantum programming:

## The Quick and Dirty Guide to Quantum Programming:

Just like classical computers, quantum computers have logic gates which form the basis of the computer program. Quantum effects like superposition and entanglement change the function of the logic gate, so for ease, I'll state what the gate does outside of superposition first. We'll dig more into what these terms mean later.

Let's start with the logic gates. On most quantum GUI's, you'll find a series of blocks with letters. On workbench, you can see the gates on the right side of the screen.

The X-gate is the equivalent of a NOT gate. It flips the qubit from 0 -> 1, and 1 -> 0. Similarly, the Y-gate acts the same way outside of superposition.

The Z-gate is a little trickier, it flips the phase of the qubit. Meaning, your (qu)bit, if flipped to 1, flips to -1 when the Z-gate is applied. This is something unique to a qubit, its phase can be changed. This is hard to imagine outside of superposition, more on this later.

Let's dive into controls. They're more like commands, specifically, they mirror the if-statement in classical computing. A white dot is an anti-control, it states if said qubit is == 0, then apply the commands connected to me. A control, black dot, does the opposite, if this qubit is == 1 apply commands connected to me.
A common connection is adding an X gate below or above the control - known as the CX gate. Simply, if (control or anti-control) == TRUE apply the X-gate, else do nothing.
Any gate can be attached to a control or anti-control.

So we've covered some basics outside of superposition, now for some quantum effects to add speed to our code.

Quantum computers get their speed up from interference, or when bit strings cancel out to get to your solution faster. So if a string == 0101, and you also have a negative 0101 in your code, it will cancel out like magic. This is where you, as a quantum algorithm designer, get to optimize your code. Let's see how this happens. But first, we need to dive into what these gates do inside of a superposition.

To create a superposition, we apply the H-gate. This is a purely quantum mechanical operation, where you have a combination of all possible combinations. So if you have 3 qubits: 000, if you place the H gate on all 3 qubits, you get:
000 001 011 100 110 101 010 111

Each can be assigned a different value. Pretty cool.

The gates behave very differently inside a superposition state, lets start with the X-gate. Each superposition value is assigned a probability, known as an amplitude. Think of it as a number between 0 and 1 that represents the probability of measuring a certain bit string (ex.0.2 == 20% chance). If a qubit string is amplified, then we know that it will have a high amplitude, say 0.9 (90%). So if a superposition state is:
0.1|0> + 0.9|1>, you know that there is a 90% chance of measuring a 1. The X-gate here flips the amplitude, so you get: 0.9|0> + 0.1|1>.

I've mentioned measurement already, but its important to clarify a little bit out the role of measurement in programming. Often times we don't need to know all qubit outputs, we simply need a few. In this case, we apply the measure gate to specify which qubits we would need as our output. Some GUI's provide a measure gate (workbench does not at the moment) for convenience. In the end, the goal is always to amplify the correct answer. Back to gates!

The Y-gate maps our (qu)bit from 0 to 1, and from 1 to -i0. The 'i' here is the imaginary unit 'i'. So the amplitudes are also flipped but watch for where the negatives apply when debugging your code, since -0 doesn't exist, it often pops up in more subtle rotations.

Finally the Z-gate, flips the phase of your superposition state. So say you have a (0+1), it ends up giving you a (0-1). It maps 0 to 0, and 1 to -1.

### To review all logic gates...

..Its useful to look at the qubit as a 3D circle, a Bloch Sphere. Test out the basic gates as well as other gates by clicking here and pressing the "B" button on your keyboard at the homepage, that will boot up the unity engine in a new tab, and show a visual qubit with all logical gates. (A little easier to imagine what's happening when you drop a gate.)

### And Back to Quantum...

Controls also do a little more inside superposition, they create entanglement, or a correlation among your qubits, and they render some states non-existent. An excellent logical operation to solve problems in shorter steps, or to check if some states exist or not (like a BOOL function). So if you have a 0, place it in a superposition (0+1), and place a CX gate with a qubit not in superposition (0+1)*0, you end up with the state:

00 + 10

Next, apply the X from the CX on the second qubit, and it becomes:

00 + 11

Now, the states (10) and (01) are non-existent. Lets apply another X just for fun to the second qubit, it becomes:

01 + 10

You get the idea. So how do we combine these gates to get a speedup?

Let's do a small demonstration of interference (you can follow along here.

First drop an X-gate on the second qubit (the second line) to flip it to one.
Next, let's drop two H-gates, one on the first qubit, another below it on the second.
Next, drop a control gate (black dot) on the first qubit, and an X gate right below it on the second qubit.
Finally, we end by collapsing the superposition state, add two H-gates like we did before - one on the first, another right below on the second qubit. And run your code.

###### Your code should look like this

You should see them evaluate to 11 (on workbench you read the output from right to left, so the two rightmost numbers should be 11). Lets go through the math to see how this happened.

###### The result

We started with one qubit set to 0, another set to 1, then we added H-gates. When added to a 0, it becomes a (0+1), for a 1, it becomes (0-1). Multiply and we get the following bits:
+00 -01 +10 -11

Next we applied the CX gate, and all stays the same except the last two (remember, if first qubit == 1, apply the X-gate, else nothing):
+00 -01 +11 -10

Finally we collapse it. Open up all the (qu)bits with the two H-gates and multiply them out to get:
+00 == +00 +01 +10 +11
-01 == -00 +01 -10 +11
-10 == -00 -01 +10 +11
+11 == +00 -10 -01 +11

You might notice why you ended up with 11. All the states cancel out because there are an even number of positives/negatives of all bit strings except for +11, it is amplified.

Most quantum programs are designed this way, except its a matter of keeping track of of a large superposition state and designing your code to output the correct answer. State of the art is art ±50 noisy qubits, which gives us over a trillion states! (2^50)

Congratulations, You've written your first block of quantum code!

Hopefully, that wasn't too difficult, because there's a lot more to it, but these are the bare basics.

One concept I didn't touch on is the ability to uncompute. You can reverse your gates in the same order you placed them to uncompute. This means two H-gates back to back will revert you back to a non-superposition state. This applies to all gates. two CX's, two X's, etc, it will all revert back to the state before you applied the operation.

With Workbench, you can write code for upwards of 100 qubits, and maybe even be the first on earth to write a useful quantum computer program (no one has figured this out yet).

If you're interested in taking your skills further, 8bitquantum.com has a set of exercises for training, and a more detailed explanation. (Just a reminder, press the 'B' button your keyboard for the 'Easter egg'/ cheat sheet!).

There are a few other gates we did not cover here such as the T-gate and S-gate, both can be modeled at the cheat sheet.

Other operations include:

The Swap gate: Swaps two qubits. A more detailed account of how it works can be found here.

Fredkin gate: A controlled-Swap (CSWAP).

Toffoli Gate: Two controls and an X (CCX).

And a few more, which can be found at the Wikipedia page of quantum logic gates. Most gates such as the CX and U3 are universal, and almost all quantum algorithms can be compressed down into these two gates efficiently to be run on near-term hardware.

## Quantum for All

I hope this post has helped you understand a little more about quantum computer programming, and I hope it's got you excited. A lot of the content out there is very much tailored for people with a deep background in math, whereas here I'd like to demonstrate that quantum computing is open for everyone, and you don't need a PHD to write code for these computers. Hopefully you're inspired to dig a little deeper, and maybe take on the challenge of writing useful code for our future.

If you've made it this far, you should be proud. You can now safely say that you have programmed a quantum computer.

Posted on by:

### sal-jim

QC/WebAssembly/Frontend among other things.