#showdev #react The Beginner's Quick and Dirty Guide to Quantum Computer Programming
Tags: showdev, saas, startup, react
Published: 2020-09-03 22:10:56

### __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](https:elyah.io/product) 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).

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wgeymjg4xrvlc1v4ipto.png)

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](https://elyah.io/workbench/lite), 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](https://en.wikipedia.org/wiki/Imaginary_unit#:~:text=A%20simple%20example%20of%20the,and%20fundamental%20theorem%20of%20algebra) '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](8bitquantum.com)__, 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](https://elyah.io/workbench/lite]).

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
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/752xtefl44n3qprh1cru.png)



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
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/a4i3gjmpe3khiip60ltp.png)



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](https://algassert.com/post/1717).

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](https://en.wikipedia.org/wiki/Quantum_logic_gate). 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_.


######_Please visit elyah.io/product for more details on workbench._