<![CDATA[Eric Hamacher's blog]]>https://hamacher.cloudRSS for NodeTue, 25 Jun 2024 19:30:39 GMT60<![CDATA[My Quantum Spin: Qubit Ordering and Operations]]>https://hamacher.cloud/my-quantum-spin-qubit-ordering-and-operationshttps://hamacher.cloud/my-quantum-spin-qubit-ordering-and-operationsSat, 16 Mar 2024 13:35:54 GMT<![CDATA[<h1 id="heading-overview">Overview</h1><p>Picking up from <a target="_blank" href="https://hamacher.cloud/learning-quantum-computing">Learning Quantum Computing</a>, I continue chronicling my learning journey through quantum computing.</p><p>This post serves two purposes. First, I had a personal a-ha moment while manually working through the math to find the resulting statevector of a simple circuit. Namely, how to correctly and consistently compute tensor products of unitary matrices. Second, in association with that moment, I investigated the ramifications of how one may order the qubits in a circuit.</p><p>In this post, I will be working with the two similar but slightly different circuits shown here:</p><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1710589880654/48490986-9766-4b39-a8ce-d53610bdc2ec.png" alt class="image--center mx-auto" /></p><p>The difference resides in the order of the qubits. The first qubit, q0, is on top in the first diagram and on bottom in the second. Why is this important? In all of my learning resources, except one, the first qubit is on top. The exception insisted on putting it at the bottom. As a beginner, the situation bothered me. How could I reconcile this in my mind? Stay tuned.</p><h2 id="heading-first-circuit">First Circuit</h2><p>Let's start by examining the first circuit. The circuit has no special significance (that I know of). It is simple enough to demonstrate what I learned.</p><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1710274754403/58ecf046-9fc4-436c-bada-2ff21e99bfc2.png" alt class="image--center mx-auto" /></p><p>As stated, it is the qubit order I see in most learning resources and diagramming tools: top to bottom. In this diagram, q0 is the first qubit, q1 is the second, and q2 is the third. I want to find the resulting statevector of this circuit. The correct answer depends on correctly defining the intermediate unitary operations.</p><p>I placed a barrier down the middle of the diagram to cleanly separate the operations. The operations before the barrier may be considered to be executed simultaneously. They consist of a Pauli-Z gate on q0, a Hadamard gate in q1, and no operation on q2. The "no operation" can be represented mathematically by an identity gate. To consider the effect of these simultaneous operations on the circuit we need to take a tensor (Kronecker) product of including Z, H, and I (the identity operation). One might go ahead and do this:</p><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1710285387459/92c303ff-099d-47b7-8d42-6f0b3492c272.png" alt class="image--center mx-auto" /></p><p>This will eventually lead to the wrong statevector. The correct product is:</p><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1710590286706/dfceaf57-7fc3-4c23-9675-d2fa2488aadb.png" alt class="image--center mx-auto" /></p><p>How do we keep this straight? Remember that the system's initial state is |000> where the rightmost digit represents the first qubit (q0). The order of arguments in the tensor product is the same from right to left. This was my a-ha moment.</p><p>How about the effective operator to the right of the barrier? This time we have a multi-qubit operation, the CNOT gate. The operation on that side of the barrier is:</p><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1710286973366/9e994d3d-0fe1-44bb-9383-f63bdba2092e.png" alt class="image--center mx-auto" /></p><p>Based on the previous IHZ operation this seems to make sense, the operation on q0 is the identity matrix and it is the righthand argument. Following the math through from the initial state, we get a final statevector of:</p><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1710287747299/e702a5ab-1cfa-4c6a-921b-ace63e65713c.png" alt class="image--center mx-auto" /></p><p>This is the correct result. The result makes intuitive sense with the only possible states being |000> and |110>. Qubit q0 only had a phase flip and qubits q1 and q2 are entangled in a Bell state. All we had to do was remember the correct ordering of the operators.</p><h2 id="heading-second-circuit">Second Circuit</h2><p>Things get interesting when we reverse the numbered order of the qubits:</p><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1710288853746/c0797b7b-eb88-46b2-a608-849bdfbf3444.png" alt class="image--center mx-auto" /></p><p>Now q0 is on the bottom. Again we start from the initial |000> state. Let's look at the resulting statevector first before we look at the math that gets us there:</p><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1710289502069/609634f1-f629-47b9-b468-281c96665572.png" alt class="image--center mx-auto" /></p><p>There might be momentary shock due to the different result but again it is easy to see why this makes sense. The possible states of the system are |000> and |011. The first two qubits, q0 and q1, are in a Bell state while the third qubit q2 only experienced a phase flip. The result is different from the first circuit but consistent.</p><p>Evaluating the circuit, we start to the left of the barrier just as we did previously. This time, the ZHI tensor product is the correct operator:</p><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1710590503843/c561ff02-49c9-44b1-9a27-6646f1703356.png" alt class="image--center mx-auto" /></p><p>Following the principle according to the ordering of qubits I discovered, we should expect the effective operator after the barrier to be:</p><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1710590954810/7325c373-43f4-421f-a2bb-15c941d8220e.png" alt class="image--center mx-auto" /></p><p>This looks good but wait, that isn't the same CNOT matrix we used in the first circuit. Here are the two CNOT matrices used in the first and second circuits, respectively:</p><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1710591453597/c0d0d4e8-51e6-460f-b4b2-037131bd98e0.png" alt class="image--center mx-auto" /></p><p>How am I going to remember which to use? The rule of thumb states that if the first qubit in CNOT is the control, use the first matrix. Otherwise, if the first qubit is the target, use the second matrix. Yuk. I wasn't satisfied. A formal mathematical derivation would be better. Moreover, what about the following situation?</p><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1710593218253/fa299401-c7ca-486e-a38a-dcc046803642.png" alt class="image--center mx-auto" /></p><p>The CNOT operator to be applied after the barrier now involves all three qubits! There is no tensor product to express the operator. Of course, we may imagine more complex situations with more qubits involved.</p><p>To answer my questions, I found the following Stack Exchange threads to be helpful:</p><p><a target="_blank" href="https://quantumcomputing.stackexchange.com/questions/5179/how-to-construct-matrix-of-regular-and-flipped-2-qubit-cnot">https://quantumcomputing.stackexchange.com/questions/5179/how-to-construct-matrix-of-regular-and-flipped-2-qubit-cnot</a></p><p><a target="_blank" href="https://quantumcomputing.stackexchange.com/questions/4252/how-to-derive-the-cnot-matrix-for-a-3-qubit-system-where-the-control-target-qu">https://quantumcomputing.stackexchange.com/questions/4252/how-to-derive-the-cnot-matrix-for-a-3-qubit-system-where-the-control-target-qu</a></p><p>Memorizing multi-qubit matrices will lead me astray. I need to formulate the correct matrix based on the situation.</p><h1 id="heading-conclusion">Conclusion</h1><p>Strictly speaking, a practitioner may rely on simulators to derive statevectors and the operators needed to achieve them. I always want to know why something works so doing the math is valuable. I hope this post was thought-provoking.</p>]]>https://cdn.hashnode.com/res/hashnode/image/upload/v1710595816135/1056b265-05ef-4584-9de7-344da3989613.jpeg<![CDATA[Learning Quantum Computing]]>https://hamacher.cloud/learning-quantum-computinghttps://hamacher.cloud/learning-quantum-computingMon, 26 Feb 2024 20:13:19 GMT<![CDATA[<h2 id="heading-overview">Overview</h2><p>Quantum computing technology is poised to revolutionize the way we solve complex problems beyond the grasp of traditional computers. The technology grows daily as computers become larger and quantum algorithms more sophisticated. Quantum computers will profoundly influence Artificial Intelligence, cryptography, and optimizations and aid scientific discovery such as predicting how a protein will fold.</p><p>Recently I began a journey to learn the basics of quantum computing. Along the way, I evaluated many educational resources. I want to share my findings so far.</p><p>Learning quantum computing is a challenge in part because it is different from current, traditional computing. No matter how big or small, today's computing devices all operate on electrical circuits. Quantum computers at their core do not. They exploit the principles of quantum physics such as superposition and entanglement to provide much more efficient computations. Quantum computers will not replace today's computers but complement them with their ability to solve complex problems relatively quickly.</p><p>In this article, I outline the main areas one may study to get started designing quantum algorithms along with resources containing information I found useful. No single source of information is exhaustive. Moreover, different resources offer varied perspectives. The different angles complement one another and fortunately, rarely contradict. </p><h2 id="heading-areas-of-study">Areas of Study</h2><p>I broke down this article into the following areas of study for the beginner:</p><ul><li><p>Overview of quantum physics</p></li><li><p>Necessary Math</p></li><li><p>Explanation of qubits and gates</p></li><li><p>Programming languages and libraries</p></li><li><p>Running on simulators and quantum hardware</p></li></ul><p>I will cover more advanced topics such as algorithms, machine learning, and quantum cryptography in later articles. I found it essential to become comfortable with these concepts before I paused to write an article. </p><h3 id="heading-overview-of-quantum-physics">Overview of quantum physics</h3><p>The word 'quantum' has grown in popularity. It is used as an adjective for many ideas and products. Of course, not everything on the market has an association to the physics. Quantum computing has certainly earned the privilege to use the word as the computational model is based on quantum physics.</p><p>I came into my study of quantum computing with a layman's understanding of the physics. I have read many popular books on the subject and am intrigued by the philosophical implications. I have a science background insofar as I have an engineering degree. So I felt comfortable with the concepts.</p><p>Many physical concepts in quantum physics have no everyday counterpart to help explain their behavior. They make no sense on the surface yet they predict the behavior of the natural world brilliantly. Most resources I digested give a brief overview of superposition and entanglement but assume you are comfortable moving ahead.</p><p>I want to single out one particular resource that dedicates much time to introducing the physics. Udemy offers a course entitled <a target="_blank" href="https://www.udemy.com/course/qc101-introduction-to-quantum-computing-quantum-physics-for-beginners/">QC101 Quantum Computing & Intro to Quantum Machine Learning</a> that gently introduces superposition and quantum measurements using photon polarization. They even encourage the reader to purchase inexpensive equipment as an aid to observing the strangeness of quantum physics firsthand. It's a great introduction to prepare you without getting into the weeds. The video course was one of the first sources I used to learn quantum computing and I will mention it again later.</p><p>Wait though, do you need to understand these physical concepts to do quantum computing? I believe the answer is a definite yes. Even if the concepts defy common sense, they need to be accepted by the student as they drive to the very heart of understanding quantum computing. </p><h3 id="heading-necessary-math">Necessary math</h3><p>It is impossible to do anything useful in quantum computing without having the mathematical background for support. Luckily, there are many resources out there to teach you the required concepts. As you move deeper into quantum computing more advanced math is necessary.</p><p>This might sound dreadful for many readers. But there is no way around it. However, I believe anybody who had an aptitude for math in high school can do well with a little patience. Before starting, I hadn't done any math since college and most of the required math for quantum computing was new to me. With exposure to the same ideas from different resources, the importance of the concepts gels. I also find the math beautiful.</p><p>The essential math requirements to start are linear algebra, Boolean algebra, probability, and complex numbers. All of these areas intersect at quantum computing. Emphasize linear algebra. It goes far beyond anything learned in high school.</p><p>Just as the resources I digested varied on the depth of quantum physics's basics, they also greatly vary on what they assume you already know about the math. Consequently, some resources were more useful to me at the very beginning. Then I started to become comfortable with other resources that picked up the pace. Therefore, I will introduce sources based on points on the learning curve.</p><p>The aforementioned Udemy course <a target="_blank" href="https://www.udemy.com/course/qc101-introduction-to-quantum-computing-quantum-physics-for-beginners/">QC101 Quantum Computing & Intro to Quantum Machine Learning</a> should be near the beginning for people learning the required math. The author patiently and gently introduces all the essential requirements listed above. Take good notes as the information grows over time. The points learned here will be reinforced by other sources.</p><p>I found a wonderful book called <a target="_blank" href="https://www.amazon.com/Essential-Mathematics-Quantum-Computing-complexities/dp/1801073147/">Essential Mathematics for Quantum Computing: A beginner's guide to just the math you need without needless complexities</a> by Leonard Spencer Woody III. It leans in a little deeper. Among the subjects are: vector spaces, linear independence and transformations, linear basis, orthonormality, matrices, complex numbers, Eigenvectors and Eigenvalues, types of operators (Hermitian/Unitary among others), Bra-ket notation, and matrix decomposition. Explaining all these concepts in a book is daunting but the author manages it well. I understood most of it but had to reread many sections to let it sink in.</p><p>From there, other resources I used assumed a bit of prior knowledge. Armed with knowledge from the previous resources, these reinforced what I had already learned and led me even deeper. IBM is a great resource for everything quantum computing. They provide a comprehensive educational track. I want to point out their introductory course <a target="_blank" href="https://learning.quantum.ibm.com/">Basics of quantum information</a> that presents quantum computing concepts with a heavy emphasis on math. The math is more challenging than other resources but worth every second. The instructor punctuates concepts with interesting mathematical proofs. Again, it assumes some prior math knowledge but you should have it by this point.</p><p>Personally, the math required for quantum computing is the most challenging aspect of learning. I still struggle with some concepts. I believe they will eventually come to me over time. I find it helpful that many resources of learning tend to repeat many of the same concepts. </p><h3 id="heading-explanation-of-qubits-and-gates">Explanation of qubits and gates</h3><p>The basic unit of information in common (classical) computing is the bit. In quantum computing, qubits hold information. Gates manipulate bits in classical computing to produce desired outcomes. Gates (in gate-based quantum computers) are also used in quantum computing to manipulate qubits. However, they are vastly different. The most common type of quantum computers are gate-based thus almost all sources of information relate to that paradigm.</p><p>At the least, most resources I encountered and studied explain the basics of quantum gates. Again, resources vary in depth of explanation. Some are more suitable for beginners than others. I found an impressive introduction to qubits and gates in the LinkedIn Learning course <a target="_blank" href="https://www.linkedin.com/learning/quantum-computing-fundamentals/learn-quantum-computing">Understanding Quantum Computing</a>. The instructors seem to anticipate any difficulties users might have in understanding the concepts. The Bloch Sphere is a powerful tool to understand the state of a qubit. But it can be difficult to explain. The authors take a novel approach to teaching the Bloch Sphere by using decorated beach balls. They proceed to use the balls to introduce how each gate affects a qubit. It is compelling and worth beyond the price of the platform.</p><p>I experienced some very artful and nuanced ways of explaining quantum gates. For instance, check out the website <a target="_blank" href="https://quantum.country/">Quantum Country</a>, a must-have resource for all beginners. The website contains a series of articles, the first being 'Quantum computing for the very curious'. The article gradually builds up the reader's knowledge of gates by presenting them within the context of related quantum concepts. The articles are extremely well-written and employ a learning technique called 'spaced-repetition' to embed concepts in the reader's mind.</p><p>A successful quantum developer knows how to use quantum gates to build up 'circuits'. Similar to classical computing, these circuits run algorithms to obtain desired results. Resources for learning algorithm design are beyond the scope of this article, however, I will cover them in the future. I still have more to learn! </p><h3 id="heading-programming-languages-and-libraries">Programming languages and libraries</h3><p>In this article up to now, my audience (probably mostly software professionals) has learned subjects in which they may not be too familiar. Let's take a moment to talk about something we all love, programming.</p><p>How do mere mortals instruct quantum computers what to do? After all, we know in essence they do not use electrical circuits for computations. Therefore, can we use traditional programming languages? The answer is yes. Quantum circuits can be coded in a variety of popular languages and platforms.</p><p>The code we write requires transpilation to an assembly language understood by quantum computers. A popular open standard assembly language understood by many quantum platforms is OpenQASM. Fortunately, established libraries will transpile for you.</p><p>Among the popular higher-level languages are Python and Q#. Python users can choose between a few libraries but the most popular is <a target="_blank" href="https://www.ibm.com/quantum/qiskit">QIskit</a> from IBM. Most sources I reviewed provide code examples using Qiskit. Another option for Python is <a target="_blank" href="https://quantumai.google/cirq">Cirq</a> from Google. For those who want to develop against the Azure Quantum Development Kit, <a target="_blank" href="https://learn.microsoft.com/en-us/azure/quantum/overview-what-is-qsharp-and-qdk">Q#</a> will be the language of choice. A couple of sources I encountered provide introductory tutelage in Q#, the often mentioned <a target="_blank" href="https://www.udemy.com/course/qc101-introduction-to-quantum-computing-quantum-physics-for-beginners/">QC101 Quantum Computing & Intro to Quantum Machine Learning</a> and a Pluralsight course <a target="_blank" href="https://app.pluralsight.com/library/courses/quantum-computing-getting-started-q-sharp/">Quantum Computing: Getting Started with Q#</a>.</p><p>Amazon Braket is a service in the Amazon Web Services cloud allowing access to a variety of quantum vendors. We will talk about Braket later. For now, I want to mention the service has its own Python library <a target="_blank" href="https://docs.aws.amazon.com/braket/latest/developerguide/api-and-sdk-reference.html">Amazon Braket Python SDK</a>. The beauty of this library is that it will transpile to the necessary assembly languages required by the devices Braket supports. AWS maintains a library to convert Qiskit to the Amazon Braket Python SDK.</p><p>Up to now, I have used Qiskit for creating quantum circuits (I did dabble in the Amazon Braket Python SDK, too). Most educational sources use Qiskit so it has been a natural choice. Another incredibly useful tool for me is Jupyter Notebooks. They allow Python users to create inline visualizations of quantum circuits, Bloch Spheres, histograms, and statevectors. </p><h3 id="heading-running-on-simulators-and-quantum-hardware">Running on simulators and quantum hardware</h3><p>The prospect of executing on actual quantum hardware helped spur me to keep learning. When I started my studies, I believed renting time on an actual device would only be possible for researchers or people with money to burn. It turns out, anybody can execute quantum circuits on real hardware for no cost or little cost.</p><p>However, quality tends to correlate with price. Qubits are highly sensitive to external disturbance, therefore, all executions deal with noise that shows up in results. More expensive quantum processing often has more sophisticated noise-reducing technology.</p><p>Furthermore, if one needs to develop a probability distribution they will need to repeat the circuit over and over (the increase of 'shots'). This also contributes to processing costs.</p><p>Libraries and platforms provide quantum simulators to test circuits before execution on actual physical devices. For learning, this is perfect as initial experimentation usually involves a low number of qubits. If you are using many qubits, say 50, a simulator will not help as such a circuit will be too compute intensive.</p><p>I perform most of my live executions on IBM quantum computers. They provide a generous amount of free processing time (10 minutes per month) on a subset of their fleet of hardware. Again, great for learning. Go to the <a target="_blank" href="https://quantum.ibm.com/">IBM Quantum Platform</a> to sign up. The documentation will show you how to run your circuit. In my experience, most submitted jobs will run within an hour. As I already mentioned, IBM also provides educational resources in the form of video courses. All of this is tough to beat.</p><p>Earlier I mentioned Amazon Braket as a service allowing access to a variety of quantum computers. The list of available vendors and devices changes periodically. This makes it a great way to experiment with different architectures. Whether or not you use Braket, I encourage readers to pick up <a target="_blank" href="https://www.amazon.com/Quantum-Computing-Experimentation-Amazon-Braket/dp/1800565267/">Quantum Computing Experimentation with Amazon Braket: Explore Amazon Braket quantum computing to solve combinatorial optimization problems</a> by Alex Khan. While the examples are specific to Braket, the author touches on the general types of quantum computers such as gate-based, annealing-based, and neutral atom-based. He provides a chapter exclusively on quantum annealing. Most valuable to me are the nuggets of wisdom for dealing with the idiosyncracies of individual devices. It is good general information. I will continue to use this book in my further studies as it is rich in information.</p><p>Running circuits on real quantum hardware is thrilling even if the circuits are only learning examples. So far, I have picked valuable information that can only be earned by actually doing. As time passes, devices are getting bigger (more qubits) and more reliable (less noise). Hopefully, it will lead to more economical processing. </p><h2 id="heading-conclusion">Conclusion</h2><p>I hope this article provides direction to those eager to learn quantum computing. Subsequent articles will document my journey. Each day, the material continues to become more challenging. My achievements will be harder earned. In the big picture, I am a novice and that's perfect for now.</p>]]>https://cdn.hashnode.com/res/hashnode/image/upload/v1708976685539/172d21d2-495c-471e-9f17-41ffece9e017.jpeg