Circuit visualization of answers to Single-Qubit Gates Part of Basic Gates Q# Quantum Kata with Quirk simulator
Introduction
Abstract
It may seem not obvious for a beginner, how to start working with a circuit simulator to reflect the code of the Katas. This article aimed at assistance in passing this step of understanding the fundamentals of Quantum Computing. This article is limited to Single Qubit Gates section of Basic Gates Kata and skips starred tasks as it may be not obvious how to visualize their angle parameter. Those who are aware already about the Quantum Katas and Quirk simulator can just scroll down to animated screenshots of visualization and open the links beside to see an appropriate Quirk circuit in action, review and play with it, or even hack it to research the behavior of circuits.
The Quantum Katas
The Quantum Katas are a series of self-paced tutorials to help you learn quantum computing and Q# programming. They are available here at Github as an open-source repository.
The author got to know about the Katas from unforgettable presentations at Microsoft Ignite 2018 by Bettina Heim and Michael Beverland. There were three of them:
- BRK1130 - Explore how to develop quantum computing applications without a Ph.D. (75 min)
- THR1158 - An Introduction to quantum programming through the Quantum Development Kit and Q# Katas - Theater Demo with Visual Studio Code (20 min)
- THR1158R - An Introduction Quantum programming through the Quantum Development Kit and Q# Katas - Repeat Theater Demo with Visual Studio (20 min)
Since those times, the Katas have moved forward significantly. Particularly, the team introduced Quantum Katas Tutorials based on Jupyter Notebook. But the main idea left intact: an adept performing a kata fills in the code that better matches the task description.
This article starts a series that is aimed at those who want to imagine the states and the gates without a Ph.D., as well as the video presentations linked above. Moreover, starting this series was inspired by attending these presentations by the author in person. The links and/or screenshots of the visualizations presented here could be introduced into Katas themselves. Regrettably, the author has no good idea about the best way of how to integrate them properly, so the visualizations are released here as a separate artifact.
The code of the katas is distributed under MIT License which requires to include the full copyright notice. To keep the classical formulation of the katas, this article contains fragments of their code, so here we go.
// The Quantum Katas. Fragments of canonical code and comments. // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license.
How to visualize
We select Quirk simulator to visualize the circuits demonstrating Kata tasks solutions.
Why Quirk? Microsoft Docs contains perfect documentation on Q# language and Quantum Computing basics and fundamentals, but... No visualization engine provided by Microsoft to simulate even simple circuits. We stick with Quirk here as it is recommended for using directly in the Theory section of Basic Quantum Katas Jupiter Notebook.
Quirk
Quirk is a drag-and-drop quantum circuit simulator. It supports 16 qubit wires with sequential gates and pseudo-gates (displays). This simulator works as a web-browser-based client-side application. All its features implemented with HTML/CSS/javascript. Particularly, circuits can be exported as JSON.
Can it be used as an official visualizer of Quantum Circuit for Q# and Microsoft Quantum Development Toolkit? One impediment in this way is the fact that there is no direct mapping from Q# intrinsic operations to Quirk gates. Particularly, Quirk toolboxes do not contain a gate for Q# R1 operation which is used in some solutions to tasks of the Kata discussed here. The author believes that some visualizer is being developed somewhere deep inside of Microsoft for better matching Q# and topologic quantum computations. Otherwise, it would be a good idea to invest in adapting Quirk to better reflect Q# instructions.
Quirk simulator, as well as some others of the kind, leads us back to the times of computing circuit design. From Computer Engineering courses, we can recall such terms as comparator, multiplexor and other arithmetic and logic gates. All of those are multi-bit, though.
In classical computing, there is only one non-trivial single-bit gate, namely inverter which just flips a bit from 0 to 1 or vice versa. In quantum computing, a lot of single-qubit operations possible. Leaving aside the trivial basis I gate (identity operation, no-op), there are basis Pauli X, Y, Z gates based on the respective matrices. Furthermore, many other well-known, special and custom gates transforming one qubit state to another can be constructed on that basis. This article can help make them out and organize visual and conceptual imagination of them in mind.
Animation of the unknown qubit state is the key point of this visualization. Animated screenshots are taken with Capture to a Gif Chrome extension. The outcome needed size optimization which was done online at ezgif.com.
Improvement, extension and contribution
Suggestion of better visualization are welcome. Also, the code of the visualization in this article can be published at github for collaboration and community contribution. Please feel free to let me know if you are interested in such publication. I believe that at some stage it can be integrated into the Katas themselves as a contribution. Any suggestion for the best way of this collaboration and integration is welcome as well.
Basic Gates
// "Basic Gates" quantum kata is a series of exercises designed // to get you familiar with the basic quantum gates in Q#. // It covers the following topics: // - basic single-qubit and multi-qubit gates, // - adjoint and controlled gates, // - using gates to modify the state of a qubit. // Each task is wrapped in one operation preceded by the description of the task. // Each task (except tasks in which you have to write a test) has a unit test associated with it, // which initially fails. Your goal is to fill in the blank (marked with // ... comment) // with some Q# code to make the failing test pass. // Most tasks can be done using exactly one gate. // None of the tasks require measurement, and the tests are written so as to fail if qubit state is measured. // The tasks are given in approximate order of increasing difficulty; harder ones are marked with asterisks.
Basic Gates: Part I. Single-Qubit Gates
// Note that all operations in this section have `is Adj+Ctl` in their signature. // This means that they should be implemented in a way that allows Q# // to compute their adjoint and controlled variants automatically. // Since each task is solved using only intrinsic gates, you should not need to put any special effort in this.
Task 1.1. State flip
// Task 1.1. State flip: |0? to |1? and vice versa // Input: A qubit in state |ψ? = α |0? + β |1?. // Goal: Change the state of the qubit to α |1? + β |0?. // Example: // If the qubit is in state |0?, change its state to |1?. // If the qubit is in state |1?, change its state to |0?. // Note that this operation is self-adjoint: applying it for a second time // returns the qubit to the original state. operation StateFlip (q : Qubit) : Unit is Adj+Ctl { // The Pauli X gate will change the |0? state to the |1? state and vice versa. // Type X(q); // Then rebuild the project and rerun the tests - T11_StateFlip_Test should now pass! // ... }
State flip: Quirk circuit visualization.
You can see a static screenshot of a circuit by this link as a heading image to this article. Below is an animated screenshot of that circuit.
In this circuit, the meaning of wires corresponds to the two example statements. The third wire is introduced for visualizing superposition states. It corresponds to the third statement that is included in some of the next kata tasks which implicitly presents in this task as well:
// If the qubit is in superposition, change its state according to the effect on basis vectors.
To see the content of the correct answer, you can hover the Kata answer square in "Custom Gates" section of Quirk simulator to see the popup tooltip, as it is demonstrated in the heading image.
The custom gate showing an animation of traversing superposition state is taken from Quirk example of teleportation where such gate is called "message" and symbolizes an unknown superposition state to be teleported. It does not look to pass all possible states of a qubit, but at least this is a way to demonstrate some of them lying on the trajectory of changing Bloch vector. This gate is not available directly from standard Quirk panel and only available by hacking underlying JSON after Export feature. One of its internal parts e^-iYt seem to be undocumented and not represented in toolboxes.
The labelling gates with text just do nothing. They could be interpreted as either just a comments or Message("...") function of Q#. They don't affect the state of qubits at the wires.
The Kata Answer custom gate encloses the gate that is expected as an answer to the Kata task. You can mouse-hover the gate in Custom panel to see a popup with its internals and get to know the answer to put it in code.
The Quirk intrinsic pseudo-gates (displays) show the state of the qubit at the particular wire before and after applying the gate solving the task of the Kata. They reveal the qubit state without destroying it. In simulator, we can easily display it as simulator is aware about the state it simulates. In real (non-simulated) quantum circuit, implementing such "gates" will require applying a kind of Quantum Tomography process or even impossible due to well-known specifics of quantum particles states and behavior such as uncertainty principle.
Task 1.2. Basis change
// Task 1.2. Basis change: |0? to |+? and |1? to |-? (and vice versa) // Input: A qubit in state |ψ? = α |0? + β |1?. // Goal: Change the state of the qubit as follows: // If the qubit is in state |0?, change its state to |+? = (|0? + |1?) / sqrt(2). // If the qubit is in state |1?, change its state to |-? = (|0? - |1?) / sqrt(2). // If the qubit is in superposition, change its state according to the effect on basis vectors. // Note: |+? and |-? form a different basis for single-qubit states, called X basis. // |0? and |1? are called Z basis. operation BasisChange (q : Qubit) : Unit is Adj+Ctl { // ... }
Basis change: Quirk circuit visualization.
The wires and custom gates meaning is the same as in the previous task.
Task 1.3. Sign flip
// Task 1.3. Sign flip: |+? to |-? and vice versa. // Input: A qubit in state |ψ? = α |0? + β |1?. // Goal: Change the qubit state to α |0? - β |1? (flip the sign of |1? component of the superposition) operation SignFlip (q : Qubit) : Unit is Adj+Ctl { // ... }
Sign flip: Quirk circuit visualization.
In this circuit, the first two wires are set to |+> and |-> states instead of |0> and |1> to demonstrate the static look of sign flip. The third one demonstrates superposition behavior on applying the answer, as it was done in previous tasks.
Task 1.4*. Amplitude change
// Task 1.4*. Amplitude change: |0? to cos(alpha)*|0? + sin(alpha)*|1?. // Inputs: // 1) Angle alpha, in radians, represented as Double. // 2) A qubit in state β|0? + γ|1?. // Goal: Change the state of the qubit as follows: // If the qubit is in state |0?, change its state to cos(alpha)*|0? + sin(alpha)*|1?. // If the qubit is in state |1?, change its state to -sin(alpha)*|0? + cos(alpha)*|1?. // If the qubit is in superposition, change its state according to the effect on basis vectors. // This is the first operation in this kata that is not self-adjoint, // i.e., applying it for a second time does not return the qubit to the original state. operation AmplitudeChange (alpha : Double, q : Qubit) : Unit is Adj+Ctl { // ... }
This task is skipped in this article as the author found it hard to visualize, mainly because he did not find a clear way to pass alpha parameter using Quirk means.
Task 1.5. Phase flip
// Task 1.5. Phase flip // Input: A qubit in state |ψ? = α |0? + β |1?. // Goal: Change the qubit state to α |0? + iβ |1? (flip the phase of |1? component of the superposition). operation PhaseFlip (q : Qubit) : Unit is Adj+Ctl { // ... }
Phase flip: Quirk circuit visualization.
In this visualization, you can see that the solution does not affect |0> and |1> states and noticeable rotate |+> and |-> states. You can find three representations of the solution in Custom gates panel of the toolbox. You can try them all by drag-and-drop replacing directly in the circuit and make sure that they produce the same displayed effect on the wires.
Task 1.6*. Phase change
// Task 1.6*. Phase change // Inputs: // 1) Angle alpha, in radians, represented as Double. // 2) A qubit in state β|0? + γ|1?. // Goal: Change the state of the qubit as follows: // If the qubit is in state |0?, don't change its state. // If the qubit is in state |1?, change its state to exp(i*alpha)|1?. // If the qubit is in superposition, change its state according to the effect on basis vectors. operation PhaseChange (alpha : Double, q : Qubit) : Unit is Adj+Ctl { // ... }
This task is skipped in this article as the author found it hard to visualize, mainly because he did not find a clear way to pass alpha parameter using Quirk means.
Task 1.7. Global phase change
// Task 1.7. Global phase change // Input: A qubit in state β|0? + γ|1?. // Goal: Change the state of the qubit to - β|0? - γ|1?. // // Note: This change on its own is not observable - // there is no experiment you can do on a standalone qubit // to figure out whether it acquired the global phase or not. // However, you can use a controlled version of this operation // to observe the global phase it introduces. This is used // in later katas as part of more complicated tasks. operation GlobalPhaseChange (q: Qubit) : Unit is Adj+Ctl { // ... }
In ReferenceImplementation.qs, only R2π_PauliI+ = R(PauliI, 2.0 * PI(), q); is proposed/expected as a solution:
R(PauliI, 2.0 * PI(), q);
At the same time, the kata task is actually solved by many expressions, let's list some of them (one solution per line):
R(PauliI, 2.0 * PI(), q); R(PauliI, - 2.0 * PI(), q); R(PauliZ, 2.0 * PI(), q); R(PauliZ, - 2.0 * PI(), q); R(PauliX, 2.0 * PI(), q); R(PauliX, - 2.0 * PI(), q); R(PauliY, 2.0 * PI(), q); R(PauliY, - 2.0 * PI(), q);
All of these expressions represent a full turn or turn-back of Bloch vector based on a Pauli basis gate. Each line from the code above can be validated by a specially created Binder of BasicGates.ipynb:
Global Phase Change: Quirk circuit visualization.
The author could not construct the proposed R2π_PauliI+ gate using Quirk means. This is why, he used one of the solutions that can be represented by Quirk. Namely, the R2π_PauliY- solution that equals to R(PauliY, - 2.0 * PI(), q); marked in the screenshot of Jupyter Notebook Binder as a successful is placed in the circuit by the link above.
Here we can see that the change is really not observable. As an exercise, the reader can play around with R2π_PauliX+-, R2π_PauliZ+- and R2π_PauliY+ that are already included in the toolbox and make use that in those cases it is not observable as well. It stays a conundrum though, how to visualize a controlled version to see an effect of these full turn-overs. Let's wait for implementation of visualization for later katas.
Tasks 1.8 - 1.10. Bell state change - 1, 2, 3
// Task 1.8. Bell state change - 1 // Input: Two entangled qubits in Bell state |Φ?? = (|00? + |11?) / sqrt(2). // Goal: Change the two-qubit state to |Φ?? = (|00? - |11?) / sqrt(2). operation BellStateChange1 (qs : Qubit[]) : Unit is Adj+Ctl { // ... } // Task 1.9. Bell state change - 2 // Input: Two entangled qubits in Bell state |Φ?? = (|00? + |11?) / sqrt(2). // Goal: Change the two-qubit state to |Ψ?? = (|01? + |10?) / sqrt(2). operation BellStateChange2 (qs : Qubit[]) : Unit is Adj+Ctl { // ... } // Task 1.10. Bell state change - 3 // Input: Two entangled qubits in Bell state |Φ?? = (|00? + |11?) / sqrt(2). // Goal: Change the two-qubit state to |Ψ?? = (|01? - |10?) / sqrt(2). operation BellStateChange3 (qs : Qubit[]) : Unit is Adj+Ctl { // ... }
Bell state change: Quirk circuit visualization.
The qubits in Bell states are entangled. Basically, the Bell states are four specific well-known maximally entangled quantum states of two qubits. If we apply a gate to one of the entangled qubits, it also affects the state of another entangled qubit. This means that setting an answer gate to only one wire, we change both entangled qubits to the appropriate entangled state.
In previous tasks, the wires were independent and not entangled. We could separately act on each qubit without affecting the others. In these tasks, we start dealing with quantum specific phenomena - the concept of entanglement. With Quantum Computing, we can harness it and achieve the results that are unachievable and unimaginable in Classical Computing.
There are three Bell state change tasks. All of them start in 0th Bell state |Φ?? and need to transform this state to 1st |Φ??, 2nd |Ψ?? and 3rd |Ψ?? Bell state respectively. These tasks can be visualized in one circuit, amplitude display demonstrates the state without and with applying the solution. Three (four) animations below show how to hover or drag-and-drop the custom gates to see their structure or solve the kata task.
In the screenshot above, you can see the popup with internals of custom gate that prepares the Bell state |Φ?? for further changing. Then drag-and-drop of the 1.8 task's answer to the placeholder demonstrates the changes in the final state and its amplitudes.
In this screenshot, the answer to 1.9 task is shown. Both of the screenshots above demonstrate that the answer gate can be applied either to upper or lower wire, and the result will be the same. This means that the same answer gate can be applied either to the 0th or to the 1st qubit in the register for solving the task.
These screenshots demonstrate two solutions for the task 1.10. The screenshot above demonstrates the solution for upper wire, i.e. the 0th qubit in the register. The screenshot below shows the one for the lower wire, the 1st qubit in the register. As you can make sure, the solutions are not the same and not interchangeable.
Of course, every action shown can be repeated and verified in the simulator itself on the circuit referenced in the above link.
Conclusion
This article covers Single-Qubit part of Basic Gates Kata, even those with exceptions. The author is planning to cover more tasks and katas, especially if this release gets feedback of helpfulness and being worth the effort. The plans include Multi-Qubit Basic Gates, Superposition, Measurement and Teleportation.
Regarding the rest of the Katas, the author's vision how to do this is not formed yet. If it forms, the plans will be extended. The author hopes on further development of Quirk simulator and closer mapping of the Q# operations.