Teaching quantum computing through programming

Ryan LaRose
18 min readApr 17, 2019

--

Last week, I implemented a week’s worth of quantum computing in an undergraduate computational science course at Michigan State University. This post is a reflection of how it went, what worked, what didn’t, and what I’d do differently in the future.

TL;DR

I developed four Jupyter notebook assignments for a flipped-classroom undergraduate course, starting with bits and qubits and ending with quantum algorithms. Overall, students enjoyed learning about quantum computing and using Qiskit to program real quantum computers. About 35% of students successfully ran their first algorithm on a real quantum backend using Qiskit, and the majority said they were interested to learn more about quantum computing. Instructors interested in seeing/using the course materials can contact me at rlarose [at] umich [dot] edu.

The course I taught for a week was CMSE 202: Computational Modeling Tools and Techniques. I was a TA for this course last semester, and am currently a TA for its predecessor, CMSE 201: Intro to Computational Modeling. Apart from CMSE 201, the only pre-requisite for CMSE 202 is Calculus I. This meant I couldn’t assume any background knowledge of quantum mechanics, or even physics in general, and had to teach quantum computing from first principles. Indeed, course enrollment includes majors from finance to biology, and the bulk of the student demographic is 2nd to 3rd year undergraduates.

The course is taught in a flipped classroom, and all course materials are written as Jupyter notebooks. For each class period, students work on a “pre-class assignment” which introduces background material through text, video, and programming problems. After completing this assignment, due the night before class, students work in groups of 3–5 on an “in-class assignment” which gives them further practice. To me, the course is a pinnacle of modern education and active learning with effective use of technology. Students generally provide high reviews for the course, which introduces them to topics ranging from agent-based modeling to web scraping to machine learning.

Layout of a table in CMSE 202. These “students” are actually the course instructor and TAs, who spent some time on the quantum computing notebooks themselves. On the large TV in the background is the IBM Quantum Composer, which was used as a backup if any students had trouble submitting jobs via Qiskit. Students often use the two smaller monitors to display code to their group as they work together on assignments.

After TA’ing for the course last semester, I decided there was one key topic missing: quantum computing. This was not just a chance to spread my research passions and “train the quantum workforce,” it was also a chance to experiment with teaching quantum computing and quantum mechanics in a new way — namely, through computational science and computer programming, a venture that has become feasible with the recent development of quantum software platforms [1].

Quantum mechanics is conventionally taught through a historical lens with just enough de Broglie anecdotes and Stern-Gerlach’s to obscure the key points. While the history is interesting and clicks with some students, a much clearer, distilled version of quantum mechanics can be presented in the language of quantum information and computation, even to high school students⁰. I think the founders of quantum mechanics would have much preferred the modern language of quantum information if they had it. Topics like Bell inequalities are vastly easier to understand/teach in terms of information-theoretic concepts such as two-party games, and in general I think it’s easier to teach many key points in quantum mechanics through quantum computing. And further, I think it’s easier to teach many concepts in quantum computing through an algorithmic lens¹. A great example is the Solovay-Kitaev theorem framed as an algorithm [2].

“I have been impressed by numerous instances of mathematical theories that are really about particular algorithms; these theories are typically formulated in mathematical terms that are much more cumbersome and less natural than the equivalent formulation today’s computer scientists would use.”

— Donald E. Knuth [3]

It’s been said that you don’t fully understand something unless you can explain it to a child. For me, the modern variant of this quote is that you don’t fully understand something unless you can explain it to a computer². Computers are the ultimate child. You truly have to explain every single detail to them, leaving nothing to the imagination, making no errors. Not only that, but you also have to translate your understanding to the language(s) they speak.

While certainly not all forms of knowledge — art, history, etc. — can be tested by programming a computer, most scientific knowledge can, and certainly most computational science can. To me, this is an interesting regime to test teaching quantum computing, which is conventionally taught in physics or computer science courses with traditional-style lectures. (Or at least I’m told — I’ve actually never taken a formal course in quantum computing.)

So here’s the question I asked myself:

Can I effectively introduce quantum computing to undergraduates with no assumed physics background using computer programming? By “effectively introduce,” I mean that I wanted students to, at the end of the module: (1) understand how (classical) computers store and process information; (2) understand single qubits at the level of wavefunctions, amplitudes, and probability distributions; and (3) understand how operations on qubits can perform useful computation. More broadly, I wanted to (4) get students interested in the field and (5) get students to appreciate the ability of “algorithmic learning” — my term for learning/testing one’s understanding through computer programming.

I’m happy to say that goals (4) and (5) were generally a success, based on survey questions collected before and after the unit on quantum computing. After the unit, student’s were asked to (dis)agree with the statement: I’m interested to learn more about quantum computing. The anonymous responses shown below indicate that roughly 65% of students either agreed or strongly agreed, and roughly 11% disagreed or strongly disagreed.

1 = strongly disagree, 5 = strongly agree.

Additionally, students were asked if “algorithmic learning” could help them understand quantum computing and quantum physics both before and after the unit. The results from before (first plot below) are roughly normally distributed, but the results from after (second plot below) are noticeably skewed towards the “strongly agree” side, indicating that the unit on quantum computing helped them appreciate the value of algorithmic learning.

These responses were collected at the start of the quantum computing unit. 1 = strongly disagree, 5 = strongly agree.
These responses were collected at the end of the quantum computing unit. 1 = strongly disagree, 5 = strongly agree.

Goals (1), (2), and (3) were also generally successful. To explain how I achieved them, I’ll briefly walk through the design of each day’s assignment and my decisions behind them. Instructors who may be interested in seeing/using the assignments can contact me at rlarose [at] umich [dot] edu. (I won’t post them here because they may be used in future semesters.) I’ll also talk about survey responses from students and notable moments from class throughout the discussion.

Day 1 Pre-Class Assignment

The shortest path to a qubit is by quantizing a bit. This first involves understanding what a bit is, so students were directed to watch this excellent video by Khan academy and code.org about how information is stored in computers using bits.

After this, qubits are introduced as the “fundamental unit of information for quantum computers.” In an overview video on qubits, they are defined as vectors of complex numbers which determine the probability of measuring a particular state. Three subsequent videos then break down each topic — vectors, complex numbers, and probability — to get a working knowledge of a qubit at the end of the assignment. Along the way, programming exercises are built into the assignment to reinforce topics. For example, after watching the video on probability, students are asked to write a function that inputs a list of numbers and says whether it could be a discrete probability distribution or not. This pre-class assignment is all geared towards writing a qubit class during the Day 1 In-Class Assignment.

Generally, this pre-class assignment seemed to generate excitement and interest, based on an open feedback survey question. A sample of some representative responses is included below, copied verbatim.

I really enjoyed this pre-class Quantum computing is something I never dreamed I would ever touch and the fact we are talking about it, is the coolest thing. — Anonymous student.

this was difficult for me but a really cool concept I am excited to learn more — Anonymous student.

It was interesting, I am a bit nervous and curious and excited. — Anonymous student.

Day 1 In-Class Assignment

Screenshot of the skeleton class for a qubit. Students add methods like Hadamard, NOT, and measure throughout the assignment.

After getting introduced to bits and qubits in the Day 1 Pre-Class Assignment, students now get more practice working with both objects — in particular, through object-oriented programming. What better way to fully understand an object than creating it yourself, writing all the rules for its behavior?

“What I cannot create, I do not understand.”

— Richard Feynman

Students are asked to write a class for a Qubit which has an attribute called wavefunction. (It’s most appropriate for this to be a private attribute, enforcing the idea that wavefunctions are not physical, just something we use to calculate.² However, students in the course were not exposed to public vs. private attributes, so this was avoided). The Qubit starts in the ground state, by convention, by means of a keyword argument in the class’s constructor.

Students are then guided through a measure method. Here, object-oriented programming and “algorithmic thinking” can really help elucidate what happens in a quantum measurement, a moderately complex phenomena which is difficult to write an equation for but very natural to write an algorithm for. Of course we have the equations p(0) = |α|² and p(1) = |β|² for the nominal wavefunction |ψ⟩ = [α, β ], but there’s another step: measuring |0⟩ enforces |ψ⟩ → |0⟩, and similarly for 1. It’s thus very natural to write the measurement process as an algorithm. Pseudocode is shown below.

Writing this method, as anticipated, was the point of most difficulty for students, but everyone was able to get it after working together or asking me questions.

At this point, students can directly see how classical information is extracted from a qubit. (They wrote a Bit class before writing a Qubit class in the assignment to compare and contrast the differences. The Bit class had a measure method as well (look at the value and return it) and also a NOT operation.) Since they always measured 0 (enforced through a programming exercise + reasoning), the assignment then introduced quantum gates to generate different probability distributions. In particular, Pauli-X as the quantum analogue for NOT, and the Hadamard gate. The finale of this assignment was seeing a uniform probability distribution after creating a fresh Qubit, performing H on it, then measuring it 1000 times.

Interactions with students in class were positive and interesting. I received several foundational questions in quantum mechanics — e.g., why are amplitudes complex numbers and not real-valued?⁴— and had individual discussions with interested students for 5–15 minutes. One notable conversation quickly led to major research problems in NISQ computing, some of which I work on. This made me realize that the vast majority of people can conceptually understand quantum computing in a very short amount of time, although they may not fully understand technical details. (I’m currently designing questionnaires to probe the type of knowledge students obtained (conceptual vs. technical) from this unit.)

Day 2 Pre-Class Assignment

Sample results for the random bit generator circuit, produced in Qiskit. In the Day 2 Pre-Class Assignment, students execute this on a simulator backend. In the Day 2 In-Class Assignment, students execute it on a real quantum backend.

Now that student’s had a working knowledge of bits and qubits, it was time to see the “real deal.” The major goal of this pre-class assignment was to introduce students to Qiskit, the Quantum Information Science KIT developed by IBM, to program a real quantum computer in the next in-class assignment. I think programming a quantum computer accomplishes several things. First, it answers the question “Do quantum computers exist?” which I get quite often from people unfamiliar with the field. Second, it begs the question “Are quantum computers better than my laptop?” and leads naturally into a discussion of the current state of quantum computers — including superconducting qubit technology, noise, error, and decoherence, all of which are topics I discussed with individuals/groups. These are important points that are often missed or overlooked in several standard resources. Third, and perhaps most importantly, it’s cool to do, and a lot of fun. It really gets people interested in the field, and it’s an awesome moment when you program your first quantum algorithm.

While programming a quantum computer was a major reason for using Qiskit, it was not the only one. Qiskit is an excellent pedagogical tool for building up quantum circuits out of qubits and bits, adding in quantum gates, visualizing them in quantum circuit diagrams, and executing quantum circuits on backends.⁵ The previous in-class assignment allowed students to understand, at some level, what’s happening “under the hood” in Qiskit. Indeed, the Qubit class students wrote is a very basic simulator. While diving directly into Qiskit is probably an option, I think programming the fundamental elements of the software before-hand leads to a deeper understanding. Qiskit allows students to go much further, however, in terms of gates, multiple qubits, and other features. This works well for extending and building on knowledge, I believe — once you program one or two gates, you see how they work. Programming a bunch more is tedious, but it’s easy to understand new gates in Qiskit after programming a few yourself.

Day 2 In-Class Assignment

Quantum experiments from across the globe. Screenshot from https://www.research.ibm.com/ibm-q/technology/devices/.

Now that students could construct quantum circuits in Qiskit — and understand what was happening under the hood — it was time to program a sequence of gates to perform a useful computation. Students were first asked to perform a random bit generator (Hadamard + measure) and execute it on a quantum computer simulator. After verifying they got the same results as the previous in-class random bit generator they programmed, it was time to send it to a real quantum backend. In Qiskit, this really is about as easy as changing the backend from a simulator to a real device. While this is an exciting experience, those who have used Qiskit know that the queue can get quite backed up at times and errors are not uncommon. In one section, roughly half of the students were able to run a job and get their results back. I was fairly pleased with this number. During the other section, one quantum computer was under maintenance, one had the queue disabled, and the other returned 400 errors or GenericErrors for each job submission. Unfortunately no students were able to run on a real backend in this section.

However, all students made it through a significant portion of the intentionally long assignment. After the random bit generator, they extended this program to a random number generator, which virtually all students immediately caught onto. (Hadamard on five qubits to generate a number from 0–31, or run the random bit generator five times.) Some students were able to execute this on a real backend as well.

The final program they constructed was the quantum teleportation circuit. While some students understood this material well, it was quite challenging for the majority. As there is a non-trivial jump from the previous problem, constructing the teleportation circuit was very scaffolded with explicit instructions about which gates to put where and images of the circuit diagrams they should end up with. An example is shown below.

The circuit diagram, generated in Qiskit, that was included in students’ notebook for “step 1” of constructing the teleportation circuit. The |1⟩ state is sent as an example, and the Bell state |00⟩ + |11⟩ is introduced in a previous problem.

Along the way, the Bell state |00⟩ + |11⟩ is discussed to introduce quantum entanglement. In particular, code is provided to create this state, then students are asked to execute the circuit and are provided guided questions for reasoning about the output. A few students even executed the Bell state circuit on a real quantum backend and asked me to confirm their understanding that 01 and 10 measurement results crept in because of noise.

Example output of measuring the Bell state |00⟩ + |11⟩ on the ibmqx2 processor. A student correctly identified that 01 and 10 measurements were due to noise on the quantum device.

From talking to groups, quantum teleportation wasn’t clear to every student. Part of this reason was interpreting the output after executing the teleportation circuit was a bit confusing — several questions were coming up like “Which bit is Bob’s bit?” and “Are the bits ordered the same way in each bin of the histogram?” The idea was to see that Bob’s bit was always 1 in the four measurement outcomes, hence he deterministically had the qubit |1⟩. While I thought the notebook should have guided students through interpreting the output, this is one area that I could have improved. This leads me into the next and final section of what worked and what didn’t.

What Worked Well

I think the assignments really sparked some interest in quantum computing. In both sections, I had long discussions with several groups and individuals about my research, the state of the field, the future, Shor’s algorithm, post-quantum cryptography, and so on. Two students signed up for my QuIC Seminar at MSU, a weekly seminar on quantum information and computation.

I’m also happy with the conceptual understanding students got from the unit. My goal wasn’t to give them a complete, mathematically rigorous understanding of qubits or quantum computing, but rather provide them foundational knowledge so they could learn this on their own. In my experience, sparking interest and providing a jumping board is the best path to effective learning. I was pleased that most students agreed that the unit gave them enough base knowledge to learn more on their own.

Survey responses collected after the quantum computing unit. 1 = strongly disagree, 5 = strongly agree.

What Could be Improved

I took a gamble instructing ~30 students to submit to the IBM Quantum experience at essentially the same time. For one section it worked reasonably well (50% success) and for the other section the gamble came up short — all computers were under maintenance, had the queue disabled, or were throwing errors when submitting jobs. The backup plan of programming via the IBM Quantum Composer had the same issues. This was only a small component that failed and did not affect subsequent problems in any way, but in the future I’d think of trying to improve the success rate. One way would be to ask IBM for dedicated access to a computer for everyone in the class.

Only 35% of students received their results back from running on the IBM Q Experience. (The one student who responded “sort of” truly understands quantum computing :)

Throughout designing the assignments, I was consciously aware that for some students, this was going to be very challenging. I was worried there was going to be a divide in responses — half the students strongly agreeing (that they’re interested, want to learn more, etc.) and half the students strongly disagreeing. On the whole, I don’t think this happened, but there nevertheless were a few students who felt overwhelmed by the unit and may have had a bad experience.

Too long. I don’t really understand the point of learning this. — Anonymous student’s response to the prompt “Any other feedback about today’s assignment?” after the Day 2 In Class Assignment.

I think I could have done a better job explaining why quantum computing could be useful even if you don’t care at all about computing or quantum physics. (Indeed, I didn’t motivate this at all.) It’s not too difficult to find reasons where studying quantum computing could be useful for other fields. For example, quantum computing generalizes classical computing to a broader theory, and includes classical computing as a special case. This has happened elsewhere in physics, for example special relativity generalizing Newtonian mechanics. The idea that you’re not seeing the whole picture — but just the first term in the Taylor series — can be eye-opening in any field and lead to key insights and reflections. Another reason could be that quantum computing really makes you think about how (classical) computers work, which are fundamental and ubiquitous today. Studying quantum computing therefore gives you a better appreciation and understanding of how computers work. Additionally, it’s good practice installing and using a new Python package, which is major learning goal of CMSE 202. Using a queue-based quantum computer via an API key is very similar to using supercomputers and other remote computational resources, so students also get practice with this even if they don’t care about quantum computing.

The other notable thing I may change is the amount of material covered. Going from zero knowledge of qubits to quantum teleportation in two days is definitely doable — and I was happy with the amount of students it worked for — but can also be daunting for a fair amount of students. I debated whether to stop introducing new material after the entanglement example with the EPR pair. I decided to push it in the end, however, but most students said the assignments were too long (even some of those who finished everything).

Future Directions

I’m currently designing surveys to probe students’ conceptual and technical understanding, see if they think the quantum computing unit should be implemented next semester, and get any other feedback. (These students volunteered to give additional feedback when I asked the class.) This post will be turned into a report for my mentored teaching project at MSU, which fulfills one requirement of the Certificate in College Teaching. I’m pushing my graduate chair to design a full course in quantum computing through quantum computer programming⁶. In a few weeks, I’ll be helping as a TA at an invite-only Google conference to “teach the teachers” on using Cirq to help teach quantum computing and quantum algorithms.

It’s interesting to think about designing full courses in quantum computing via quantum computer programming, especially at more advanced levels. While I think it could be done, it would likely lead to a less rigorous understanding than could be attained in a standard quantum computing course. At some point, you need to pick up a pencil and paper and really think through the algorithm. There’s no escaping this. However, computer programming can really help explain some topics better. At some point, you need to pick up a computer and really think through the algorithm.

Acknowledgements

I thank IBM for providing access to their quantum computers. (And so do the students!) The views expressed in this post are my own and do not reflect those of IBM or the IBM Q team.

Notes

⁰ See Scott Aaronson’s transcription of teaching quantum computing to high school students: https://www.scottaaronson.com/writings/highschool.html. Here, he jumps right into complex numbers and amplitudes, not de Broglie or Stern-Gerlach. A similar approach is taken in the fantastic Quantum quest by Maris Ozols & Michael Walter: https://www.quantum-quest.nl/quantumquest.pdf. This is discussed more below in “Other pedagogical tools for quantum computing.”

¹ In David Deustch’s seminal paper “Quantum Theory, the Church-Turing Principle and the Universal Quantum Computer,” he writes out pseudocode for the EPR experiment in just a few lines. My slides from APS March — which are more focused on the design of quantum programming languages, not pedagogy — include this.

² I’ve heard a similar quote from Dr. Kaden Hazzard: “You don’t truly understand something until you can explain it to a machine.”

³ I guess this tells you what my favorite philosophy of quantum mechanics is.

⁴ See this excellent blog post by Scott Aaronson for a discussion of amplitudes and the field they reside in: https://www.scottaaronson.com/blog/?p=4021 I was aware of this article when questioned by the student but hesitated to go into details of quaternionic reduced density matrices. My final answer — and I’m not sure if its the best one — was that amplitudes are complex by trial and error when developing quantum mechanics. Complex numbers worked, so they stuck around.

⁵ It should be mentioned that Qiskit is not the only software for this — there are many great alternatives including Cirq, pyQuil, Q#, and ProjectQ. For a review, see [1]. Qiskit or pyQuil are favorable for using real quantum devices. I chose Qiskit because the way circuits are constructed is similar to how bits and qubits were programmed in the Day 1 In Class Assignment.

⁶ Similar to this course at Stanford by Will Zeng, which is currently running and looks fantastic: https://cs269q.stanford.edu/.

References

[1] Ryan LaRose, “Overview and comparison of gate level quantum software platforms,” Quantum 3, 130 (2019).

[2] Chistopher M. Dawson and Michael A. Nielsen, “The Solovay-Kitaev algorithm,” arXiv:quant-ph/0505030, May 2005.

While I love the content and perspective of this paper, my favorite part is that it solves a problem in Nielsen and Chuang. From Section 6 of [2]: “That appendix gave a detailed description of the SK theorem, and included an exercise (Exercise A3.6) asking the reader to find an algorithm for efficiently finding accurate sequences of instructions approximating a desired unitary. Reader feedback suggests that this was not one of the easier exercises in that volume, a fact that partially inspired the present review paper.”

[3] Donald E. Knuth, “Computer Science and Its Relation to Mathematics,” The American Mathematical Monthly, vol. 81, no. 4, pp. 323–343, 1974.

Other Pedagogical Tools in Quantum Computing/Quantum Physics

Andy Matuschak and Michael Nielsen’s Quantum Computing for the Very Curious is a fantastic introduction to quantum computing that utilizes spaced-repetition to improve memory. Spaced-repetition makes memory a choice by quizzing oneself at longer and longer time intervals. Check out the blog Augmenting Long-term Memory, also by Michael Nielsen, to learn more about spaced-repetition.

[Update 05–02-2019] Shortly after publishing this article, the brilliant Andy Matuschak and Michael Nielsen struck again with How the Quantum Search Algorithm Works, another mnemonic medium article, this time on Grover’s algorithm.

The Quantum Quest by Maris Ozols & Michael Walter is a fun and fantastic four week adventure into quantum computing. Extremely well-written, it starts by introducing qubits and finishes with quantum algorithms like Deustch-Jozsa and Bernstein-Vazirani. I recommended this resource to CMSE 202 students who asked “How can I learn more about quantum computing?”

The QuIC Seminar website, organized by myself, contains a collection of (mostly) free resources for learning about quantum computing. In addition to external sources — like textbooks, lecture videos, etc. — we (me + other graduate students) publish lecture notes for each weekly seminar and Jupyter notebook tutorials, when appropriate, for quantum algorithms like VQE and QAOA.

One notable resource listed on the QuIC Seminar website is A course in quantum computing (for the community college) by Michael Loceff. This volume starts from ground zero and explains concepts, algorithms, and derivations in excellent detail, really breaking down every step. There’s also video lectures for the course.

Do you know of any other studies in the pedagogy of quantum computing or quantum information? Any using quantum software platforms and/or computer programming?

--

--