Quantum Hello World

Greg Abbas

I recently discovered that it's possible to write your own quantum computer program and run it on real hardware for free! You don't have to enroll in a graduate degree program either. If that sounds interesting then read on, because this is a story about how I did it and you can too.

Background

Until recently I hadn't explored quantum mechanics much since college, because there's not much need for it in my career (software engineer). But then I got interested in it again, spurred by a desire to learn challenging things to keep my brain nimble. My activities have been mostly limited to reading, watching YouTube videos (Space Time!), and musing about foundations (i.e. interpretation of quantum mechanics).

That's been fun, because I'm intrigued by the philosophical implications it might have for the nature of reality. Probability, free will, and all of that. But then at Christmas when I was chatting with my dad and he asked me about quantum computers, I realized I didn't know nearly as much about them as I would like. So quantum computing became my new project.

A Different Paradigm

First, I needed to learn the basics. A quantum computer is similar to a conventional computer like the one you're using to read this web page, except that it manipulates a different unit of information. A conventional computer's memory is made up of classical bits (0 or 1), and the computer goes to a lot of trouble to keep them in a well-defined state, because it's not useful if a bit accidentally flips from one state to the other or gets into a state where it's not clear what value it has. The basic unit of information for a quantum computer, on the other hand, is a qubit ("quantum bit"), which is like a bit but exists in a quantum superposition of states. In addition, qubits can be entangled with each other, which is what promises to give a quantum computer the ability to solve problems that are intractable on conventional computers.

Because each qubit can do so much more than a regular bit can, you don't need nearly as many of them to solve a useful task. Whereas a modern laptop might have over 1011 bits, state-of-the-art quantum computers have about 400 qubits.

By the way I'm not going to try to make this a full tutorial on how quantum computers work, because other people have already done a way better job of that than I could. But hopefully by glossing over many of the technical details, the narrative of this little journey will make sense.

Languages and Frameworks

So what does the experience of programming such a computer look like? What paradigm do you follow to control a machine that's so different from a MacBook Pro? It's a little like programming a GPU, in that you run a program on a conventional computer that builds a description of the quantum circuit that you want to run. The host computer is tethered to a quantum processing unit, and it uses that connection to run circuits on its quantum processing unit.

The classical processing layer sends the quantum circuit that your program constructed to the quantum processing unit where it gets run, and the measurements of the quantum states (qubits) it created get transferred back to the conventional computer. The host language of choice seems to be Python, and a quantum circuit is represented as a Python data structure. That data structure can be interpreted by a simulator (running on your laptop, perhaps) until you're ready to try running it on real quantum hardware.

The frameworks I found for doing this are Google Cirq and IBM Qiskit. They both look cool, but IBM's statement that with Qiskit you can "run on real hardware" caught my eye and made me decide to use Qiskit. Maybe that's possible with Google too, but IBM seemed more welcoming.

Getting started is pretty easy if you're already familiar with Python. You basically pip install qiskit and follow their tutorial.

What does one of these quantum circuits look like? The framework gives you a couple of tools to visualize them as you go, which is great for helping you understand what you're building. Here's an example:

The idea is to declare how many qubits the circuit will have, set up their initial states, run a series of quantum gates that cause the qubits to interact with each other, and finally measure them to get results out. Measuring a quantum state is inherently random (God plays dice), and in practice a quantum computer has a certain error rate, so you run your program several times and analyze the outputs statistically.

A Software Experiment

Originally I had planned to learn a bit about quantum algorithms, and write something that uses entanglement to perform more calculations than the number of gates in the circuit. But as I was going through the tutorials, I discovered one that describes how you can use a quantum computer as a kind of "programmable physics lab" to demonstrate violation of the CHSH inequality. That's intriguing because CHSH is a version of Bell's inequality, which is the experimental test developed back in the 1960's to prove that Einstein was wrong about quantum mechanics being incomplete. I was recently having a discussion with a friend about "hidden variables" like the ones that Einstein incorrectly predicted, so reproducing this experiment myself sounded great. Also, it's an apropos topic because just last year the Nobel prize was granted to Alain Aspect, John Clauser, and Anton Zeilinger for "loophole-free" confirmation of this result.

The circuit required to do this is exceedingly simple, so it's not a vehicle for learning much about quantum algorithms. But the physics is exciting.

Real Metal

So what quantum computers are out there? Register for an account with IBM, and then check out their Compute Resources page. Of the 22 that it lists, 14 of them are available to the public. Some of those have long queues of pending jobs, so I picked one in Quito, Ecuador because it had a short queue. And because my dad used to live in Ecuador, so I thought it would make a good story to tell him. This particular computer is a Falcon r4T has 5 qubits, which is of course a lot smaller than the biggest ones in the world, but it's more than powerful enough for my little CHSH program.

Not all of the qubits can interact with each other; gates can only operate along certain connections. Here's the topology of this Falcon r4T:

For my two-qubit program, topology isn't an issue either. Then I checked the calibration data for the machine, because the error rates I mentioned above can affect how accurate the output of a program can be.

Qubits 0 and 3 look the most reliable, so I used them.

Results

With the short queue of pending jobs, I only had a to wait a couple of minutes for mine to start executing. A couple minutes after that, I had the output from my program. For fun, it ran the CHSH scenario with a variety of separation angles even though the optimal one is known to be π/4. Here's a chart of the results:

Notice how the witness function (it's a linear combination of measurements of qubits, averaged over several runs) exceeds the threshold of ±2 (shown in red) for certain angles of θ? That's what quantum mechanics predicts, and if the classicists (like Einstein and his buddies) were right, it wouldn't happen.

What Does It Mean?

So if this experiment rules out local hidden variables, what explanation do we resort to instead? That question has been discussed a great deal, and we are not close to having a consensus on it because the subject doesn't seem amenable to further experimentation. We'd like to perform experiments to rule some of them out and narrow down the list of possibilities, but the prospects of doing so don't seem promising. So for now, this is a question of philosophy rather than science.

Like I said above, it's questions like this (the interpretation of quantum mechanics) that attracted me to the topic in the first place. I have my own opinion, but I'll save that for a future article.

Questions or comments? Email me at .

2023 Apr 15