Introduction
Everybody is talking about quantum computing and how it will change the world. But what is it really? How does it work? And how can you use it?
I am the kind of person that learns better when I can touch and feel things. Today you can write code that produce quantum circuits and run them on real quantum computers via cloud providers. And you can get started without using your credit card.
Or maybe you want to play with quantum computing and then make your own laptop sticker to flex your skills?
In this blog I will attempt to show you how to get started and take the first steps within quantum computing.
I have divided the article into the following sections:
- Quantum computing – should I be interested?
- Q&A: a short introduction to quantum computing
- Superposition and Entanglement
- A simple quantum circuit (which we will run soon)
- Hands-on: a short guide on how to run your first quantum circuit on both a simulated and a real quantum computer
If you’re impatient and just want to get started, you can skip to the first 3 sections and go straight to the hands-on.
1. Quantum computing – should I be interested?
If you are interested in an area that has touch points with quantum mechanics the answer should be clear. The famous physicist Richard Feynman is often quoted on the necessity of quantum computers for simulating quantum systems. The exact quote is “Nature isn’t classical, dammit, and if you want to make a simulation of nature, you’d better make it quantum mechanical”.
But there are also other areas where quantum computing can very beneficial. One example of this is security and encryption.
As you may have heard, most of the encryption methods that are used today are not quantum-resistant i.e. they are expected to be comprised by quantum computers in the future. One example is cracking RSA encryption which is designed to take classical computers an impractical long time to solve. Quantum computers today cannot solve this problem however quantum computers are improving and there are signs that quantum computers in the future can solve this problem in a reasonable time (if you want to know more the keyword for your search is Shor’s algorithm). This is a big deal because RSA encryption is used to secure the internet.
This is a technology of the future so why worry now? One perspective is “harvest now, decrypt later” which is a strategy where the threat actor collects encrypted data with the intention of decrypting it in the future. This may be an issue if your data needs to remain secure for years, such as long-term business secrets.
Another perspective is that quantum computing is evolving at a remarkable rate, there are various types of quantum computers being developed and countries like China are investing in quantum research including for potential strategic advantages.
The exact timing of a breakthrough is uncertain, and it may happen without public knowledge, similar to the secret hacking of the Enigma machine during World War II. The British kept their success in breaking the Enigma code a closely guarded secret, using the intelligence gained to secure a strategic advantage.
A third perspective is migration time. The industry is working on post-quantum cryptography (PQC) to develop new algorithms that are secure against quantum attacks. However, for many systems, changing encryption algorithms is not easy. This means these changes need to be planned well ahead.
In addition to PQC, Quantum Key Distribution (QKD) is emerging as a promising technology for secure communication. QKD leverages the principles of quantum mechanics to enable two parties to produce a shared random secret key. However, while PQC circumvents the thread using new algorithms, QKD requires new HW.
2. Q&A: a short introduction to quantum computing
If you are interested in quantum computing (well you made it this far) the next couple of paragraphs attempt to shed some light on what a quantum computer is – the why, what and how.
Q) What is a quantum computer?
A) A quantum computer is a computer that utilizes quantum mechanics to solve complex problems.
Q) Why quantum computing?
A) Quantum computers can solve certain problems much more efficiently than classical computers. This is because quantum computers can process information in a fundamentally different way than classical computers.
Q) Is quantum computing better than classical computing?
A) Quantum computers are better at solving certain problems, while classical computers are better at solving other problems. Quantum computers are not meant to replace classical computers, but to supplement them.
Q) What are some examples of problems that quantum computers are good at solving?
A) Quantum computers are good at solving (some) problems that have an exponential time complexity on classical computers. One example is encryption as mentioned above. There are also other categories of problems that are interesting e.g. quantum computers simulating quantum systems and potentially new areas like drug discovery where quantum computers are accurately simulating molecular structures and interactions.
Q) How does a quantum computer work?
A) Quantum computers use qubits instead of bits. A qubit is the building block in quantum computing. It is similar to a classical bit, but can be in a state of 0, 1 or both at the same time (called superposition). Superposition is one of the things that makes quantum computers so powerful. Another thing is entanglement, which is a phenomenon where two qubits can be correlated in a way that classical bits can’t (more on superposition and entanglement later).
Q) Do I need to be knowledgeable at quantum mechaniscs to use quantum computing?
A) You can learn the basics of quantum computing without knowing anything about quantum mechanics – but it does help to understand some basic principles.
Q) Is quantum mechanics strange?
A) Yes, quantum mechanics are strange and hard to grasp. Richard Feynman is quoted for saying: “If you think you understand quantum mechanics, then you don’t understand quantum mechanics”.
Q) If quantum mechanics are so strange, then why do we use it?
A) Quantum mechanics is strange, but it works. It is one of the most successful theories in physics and has been confirmed by many experiments. Quantum mechanics is the foundation of many modern technologies, such as semiconductors, lasers, and MRI machines. The Standard Model of particle physics builds upon the principles of quantum mechanics to describe the fundamental particles. One of the most remarkable aspects of the Standard Model is its precision. For example, the magnetic moment of the electron, a property predicted by the Standard Model, has been measured to an accuracy of one part in a trillion. This level of precision is akin to measuring the distance between New York and Los Angeles to within the width of a human hair!
Q) Can it play CS:GO?
A) Not yet!
3. Superposition and Entanglement
Quantum mechanics is strange. It is very different from classical mechanics, which is the physics that we are used to in our everyday life.
A quantum particle is not located at a particular location but characterized by a probability distribution indicating that it is a little bit here but also a little bit over there. This probability distribution is often represented by a wave function. The wave function gives the probabilities of finding the particle in various locations. When you measure or interact with the particle, the wave function “collapses,” and you find the particle at a specific location. If you repeat the experiment many times, the locations where you find the particle will follow the probability distribution given by the wave function.
One of the most famous experiments in quantum mechanics is the double-slit experiment. If you shoot a wave screen with two slits, you would expect to see an interference pattern on the screen. This is because the wave goes through both slits and interferes with itself. However, if you shoot particles (like electrons) one by one, you would expect to see two lines on the screen, one for each slit. But that is not what happens. You still see an interference pattern on the screen.
This phenomenon is called superposition, which is one of the key principles in quantum mechanics. Superposition means that a quantum particle, like an electron, can exist in multiple states or locations simultaneously. In the double-slit experiment, each electron goes through both slits at the same time, creating an interference pattern as if it were a wave. Only when we measure or observe the electron does it “choose” a specific slit to go through, collapsing into a single state.
Superposition is one of the elements that makes quantum computers so powerful. A quantum computer can process information in multiple states at the same time. An analogy using this is solving a maze where you can go down all paths at the same time to find the solution.
Another key principle in quantum mechanics is entanglement. Entanglement is a phenomenon where two particles become correlated in such a way that the state of one particle is dependent on the state of the other particle, no matter how far apart they are. This unique property is particularly useful for solving complex optimization problems. Imagine trying to coordinate a fleet of delivery drones to find the most efficient routes. With entanglement, the drones can instantly share information and adjust their paths in perfect harmony, finding the optimal solution much faster than classical methods.
4. A simple quantum circuit (which we will run soon)
Instead of classical bits we will use quantum bits (qubits) in quantum circuits. A qubit can be in a state of 0, 1 or both at the same time (called superposition). We start with one qubit q0 in state 0 (denoted |0⟩). When we measure this qubit, it will collapse. In this case, the probability of measuring 0 is 100% and the probability of measuring 1 is 0%.
Now we add a Hadamard gate to q0. The Hadamard gate puts the qubit in superposition. The qubit is now in a state of 0 and 1 at the same time. When we measure this qubit, the probability of measuring 0 is 50% and the probability of measuring 1 is 50%.
Now we add another qubit q1 in state 0 (denoted |0⟩) and then add a CNOT gate between to two qubits. The CNOT gate entangles the two qubits. The qubits are now correlated in such a way that the state of q1 is dependent on the state of q0. When we measure q0, the probability of measuring 0 is 50% and the probability of measuring 1 is 50%. The state of q1 is dependent on the state of q0. If q0 is 0, then q1 will be 0. If q0 is 1, then q1 will be 1.
5. Hands-on: a short guide on how to run your first quantum circuit on both an emulated and a real quantum computer.
Code languages for quantum computing
The are multiple code languages for quantum computing. The most popular are Q#, Qiskit and Cirq. These languages are used to create quantum circuits that can be run on quantum computers.
- Qiskit: Developed by IBM. Widely adopted in both academic and industrial settings and has the largest community, making it the most popular among the three.
- Cirq: Developed by Google. Optimized for Google’s hardware and is also well-adopted.
- Q#: Developed by Microsoft. Has strong integration with Microsoft’s ecosystem, good for educational purposes. Is gaining traction, but it has a smaller user base compared to Qiskit and Cirq.
The languages Q#, Qiskit and Cirq all interprets high-level code and submits an intermediate representation to a provider service in form that can either be run directly on real quantum processors (QPUs) or will undergo further compilation and specialization towards the hardware before executing.
Qiskit and Cirq are Python libraries. Q# is its own code language with syntax and structure influenced by C#. Although it can be integrated with Python using the qsharp package allowing you to run Q# code within a Python environment.
Azure Quantum
Azure Quantum is portal that provides an easy way to get started with quantum computing. You can run quantum circuits on simulated and emulated quantum computers directly in your browser. Azure Quantum does a lot to get you started with quantum computing. It provides a lot of resources, tutorials, and examples to help you get started. Also it automatically connects you to your backend and visualizes the results.
If you go to the Azure Quantum web page you can experiment with Q# code directly in your browser. You have the option of using either an in-memory simulator or a statevector-based quantum emulator (also emulating noise) – both free-of-charge on the page. And as it is a Microsoft service it has Copilot integration so you have “someone” you can discuss the code and quantum circuits with.
If you want to run the quantum circuit that we discussed earlier, you can copy and paste the following code into the editor on the Azure Quantum web page:
As the output is probabilistic, you can run the code multiple times (= run multiple shots) to get a distribution of the results:
Note: |00⟩ and |11⟩ should be 50%/50% – the more shots the get better the precision.
There is no contribution from |01⟩ and |10⟩ indicating that there is no noise in this simulated system.
If you later want to “graduate” your code project to use real QPU’s on a provider that you have access to, Azure Quantum supports this by adding a few lines of code. However, running on a real quantum computer will cost you some money if you stay in the Azure realm. But you can get some free computing time if you use IBM Quantum…
IBM Quantum
IBM Quantum is another portal that provides an easy way to get started with quantum computing. If you sign up and log in, you can run quantum circuits on IBM’s real quantum computers… and you get 10 minutes of free quantum computing time per month.
Go to https://quantum.ibm.com/ and sign up for an account. You can now run your Qiskit code from your local machine using IBM Quantum computers as backend.
Go to https://quantum.ibm.com/ > Compute to see the available quantum computers. While you’re here take your API token from the Dashboard page.
You can now run your Qiskit code from your local machine using IBM Quantum computers as backend.
In your IDE, create a Python virtual environment (I’m using MiniConda here) and install a few packages:
Create a new Jupyter notebook and create a new cell containing your quantum circuit job:
If this runs without errors the job is submitted to the IBM Quantum computer. You can now go to the IBM Quantum web page to see the status of the job and the results.
You can now see the results of your quantum circuit:
Similar to the simulated circuit an Azure Quantum you now get a distribution with |00⟩ and |11⟩ being nearly 50%/50%. There is some contribution from |01⟩ and |10⟩ indicating that there is noise on the real quantum computer.
Conclusion
I hope you found this exploration of quantum computing informative and enjoyed seeing some actual code that touches on superposition, entanglement, and quantum circuits. Quantum computing is not just a fun and interesting topic, it’s also relevant for the future of technology and security.