Skip to content
Sign upLog in
← Back to Community

Quantum information theory basics and how to code a quantum computer

Profile icon
adityakhanna

Quantum information theory basics and how to code a quantum computer

In this tutorial, I'll try to go over the basics of quantum computing including gaining an understanding as to how logic works differently in a quantum computer versus in a classical computer. I wanted to make this as comprehensive as possible trying to understand quantum information theory at its core, partly to cement my own understanding through simplification and teaching and I wanted to focus more on theory than practical but skipping on to the code is fine too.

The theory - classical bits vs cubits and quantum information

A classical bit in a classical computer can take one of two states, 0 or 1. In a quantum computer however this gets more confusing with the introduction of two quantum mechanical theories which are superposition and entanglement. In this way quantum bits or "cubits" can be 0 and 1 at the same time until measured.

Superposition and entanglement

Superposition is where a cubit takes both a 1 AND a 0 at the same time, and this means that cubits are both 0 and 1 until measured at which point they collapse into one state. States in quantum mechanics are represented using Bloch spheres and are probabilistic.

Entanglement is where the state of one cubit directly affects the state of another around it at any distance which is interesting because it doesnt make sense according to classical physics laws of information transmission faster than the speed of light. It happens when two particles become inseperable linked and instantly change state in tandem at any distance. It is also known as "quantum non-locality" should you wish to know more about it.

More about cubits and their probabilistic nature

Quantum theory in general has a probabilistic nature and cubits are no different. We can represent cubit probabilities using notation known as "Bra-ket notation" or "Dirac notation" after the scientist/mathematician that first pioneered them. Bra-ket notation can display the independent probabilities of a quantum bit taking a particular form as shown in the example here.

Cannot infer image mime type

This notation here shows that there is a significantly higher probability of the cubit collapsing into a 1 when measured than collapsing into a 0. What this fundamentally means though is that a qubit (when not being observed) can lie in any probabilistic state between DEFINITELY BEING MEASURED A 1 and DEFINITELY BEING MEASURED A 0. In this case it has a much higher probability of being measured a 1 than being measured a 0.

Uses of quantum computers

Let's begin with an example, searching a database for a specific value. A classical computer could use various different search algorithm but at its core will have to check many different values for a match. A quantum computer on the other hand can have them all checked at the same time, resulting in an almost instantaneous result! Quantum computers can manage this because the number of states they can acheive at once is 2 raised to the power of the number of bits so they are amazing at many different exponential tasks. These include training complex Artificial Neural Networks, medical discovery, breaking encryptions and many more tasks that branch in to several more.

Practical - implementing quantum computing on a cloud based IBM computer

Before we begin the coding, there is one last part of theory that would be useful to go through. This is "quantum gates".

Quantum gates

Just like a classical computer uses classical AND, NOT, OR etc. gates based upon the boolean logic of its bits, quantum computers require their own gates.

I won't delve too deep on this since this is meant to simply be on the basics so we will be making a very, very basic quantum circuit and then running it on an IBM quantum computer.

First of all visit https://quantum-computing.ibm.com/composer/ (logging in with your IBMid if you have one or signing up otherwise) and experiment with the different gates and swaps in the circuit composer. You should see an assortment of gates and other operations on the top, a "Bloch sphere" representing a cubit at the bottom right and a probability bar chart on the left. I recommend that you move on to the Bloch sphere later on and begin with the bar chart.

Let's start off by using the drag and drop circuit composer to make a basic circuit:

Cannot infer image mime type

This is a simple quantum circuit and it shouldn't take long for you to reproduce, simply dragging the H (first gate from the left) and the CNOT (third gate from the left). Then drag and drop a "measure" on the q0. This acts as a quantum qubit measure which when run several times on the quantum computers shows the probabilistic pseudorandom nature of quantum computers.

Playing around with the gates is a great way to understand their workings the same way as messing up the hyperparameters on neural networks helps you understand them, but for now lets focus on what this circuit does and implementing it on a quantum computer itself.

To straight away simulate the circuit on a real-life cloud quantum computer you need to hit this button, or select whichever quantum computer you want to run it on (organised by their location). Change the name of the project to something other than "Untitled".

Cannot infer image mime type

Cannot infer image mime type

Go ahead and hit run and wait for the quantum computer to complete your request and have a look at the results.

Cannot infer image mime type

As we expected, there is a close to 50% probability of getting either 000 or 001. Because of the number of times this was run, you may get a different result. You may even get 100% of one result but this does show how they are probabilistic in nature.

Doing this using Python and Qiskit

So as you may already have noticed, your circuit gets converted to OpenQASM and Qiskit code which you can copypaste and use in a different environment. I find the circuit composer incredibly friendly and great but I feel its useful to learn Qiskit syntax nonetheless if you're planning to go deeper into quantum computing. Algorithms later on will require enormously complex circuits and a lot of mathematical algorithms in use.

Anyway in case you couldn't find it, this is the Qiskit python code for the circuit.

from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit from numpy import pi qreg_q = QuantumRegister(3, 'q') creg_c = ClassicalRegister(3, 'c') circuit = QuantumCircuit(qreg_q, creg_c) circuit.h(qreg_q[0]) circuit.cx(qreg_q[0], qreg_q[1]) circuit.measure(qreg_q[0], creg_c[0])

So how do we now run this on a quantum computer from python?
This part is incredibly simple.

Simply head to https://quantum-computing.ibm.com/jupyter and paste your code.
Then add the following lines of code to define your circuit as a job and get the result.

# imports from qiskit import Aer # run the circuit on a statevector simulator on the cloud backend = Aer.get_backend('statevector_simulator') # create the program job = execute(circuit, backend) # get the circuit results result = job.result()

In case you do want to do it in a separate environment, head to MY ACCOUNT -> API TOKEN and copypaste your API token and follow the instructions given there. I do highly recommend using the Quantum Lab notebooks because they are very helpful and friendly along the way.

Thanks for reading to this point in the tutorial. In the next tutorial I'll do in quantum computing I'll try to take you through more quantum gates and circuits using Qiskit.

Credits and further reading.
Quantum Mechanics and Quantum Computing is a very vast topic. These are just a few of the sources (not all linked) which helped me learn about Quantum Theory and Quantum Computing:

Voters
Profile icon
wjnb
Profile icon
CodeLongAndPros
Profile icon
gibbsfreenergy
Profile icon
adityakhanna