[HN Gopher] A tutorial quantum interpreter in 150 lines of Lisp
       ___________________________________________________________________
        
       A tutorial quantum interpreter in 150 lines of Lisp
        
       Author : varjag
       Score  : 167 points
       Date   : 2023-07-16 16:41 UTC (6 hours ago)
        
 (HTM) web link (www.stylewarning.com)
 (TXT) w3m dump (www.stylewarning.com)
        
       | adamnemecek wrote:
       | Recently, I have been working on some things that sometimes
       | overlap with quantum computation and I have realized that the way
       | all the quantum languages approach this wrong. If you are
       | programming explicitly with CNOT gates, you are doing it wrong. A
       | Python programmer doesn't care about logical gates, and neither
       | should a quantum programmer. To that end, I'm convinced that the
       | interplay between coinduction and induction, or coalgebra and
       | algebra is the way forward.
        
         | archgoon wrote:
         | [dead]
        
         | reikonomusha wrote:
         | This very well may be true, in the same sense that a Python
         | programmer doesn't (usually) care about assembly language. But
         | at their lowest levels, quantum computers _are_ executing a
         | sequence of gates, and much of the work and sophistication of a
         | quantum compiler is to optimize said sequences for rapid and
         | accurate execution. So while gate sequences may not be the end-
         | game to the programmers of tomorrow, they 're undoubtedly a
         | necessary component in any software stack.
        
           | adamnemecek wrote:
           | I never said that they weren't. It's just that most
           | programming language don't deal with gates as a part of their
           | API.
        
             | taeric wrote:
             | This is true, but at that point, the goal would be to have
             | a higher level language above the quantum gates. And at
             | that point, I'd guess the code is less educational?
             | 
             | That is, at the end of the day, something like Shor's
             | algorithm can be reduced to some math constructs we roughly
             | know. The speedup comes from these only being efficient
             | using quantum gates. Implementing the code using abstract
             | quantum gates isn't to try and compete, but to try and
             | understand the gates and how they work at a logical level.
             | 
             | This is like learning how boolean gates work to understand
             | some ideas of how computers work. The only people that
             | really think in many of those terms are the CPU designers.
             | Teaching the next round of the designers does so by working
             | with boolean models to get there.
             | 
             | And you are correct that we may have other quantum
             | constructs someday. Just like much of what goes into a CPU
             | isn't strictly OR/AND/etc. With the way gates are wired, it
             | can be confusing to folks as the input signal can also be
             | seen as destroyed in the circuit, but a deterministic
             | signal is captured on the other side.
             | 
             | Now, the above is all from my weak intuition here. I would
             | not be shocked to find I'm wrong on parts.
        
         | miguelmurca wrote:
         | Maybe not your point, but a lot of programmers like to take
         | this as "I should invent the Python of quantum computing",
         | which is just about as silly as trying to approach anyone in
         | 1960 and telling them they shouldn't be thinking about
         | electronics at all.
        
         | 082349872349872 wrote:
         | > _the interplay between ... coalgebra and algebra_
         | 
         | If you have not already seen it, you may be interested in
         | Squiggol (to get an idea of its age, it probably had some
         | indirect influence upon Python)
         | 
         | cf Charity:
         | https://prism.ucalgary.ca/server/api/core/bitstreams/756b50a...
        
       | robbywashere_ wrote:
       | "Thanks for interviewing with us today at xyz... please click on
       | that link I just sent you in the chat. Today we will be doing a
       | little quantum coding question. Try to speak out loud so I know
       | what your thinking and your thought process."
        
       | adamisntdead wrote:
       | I wrote something similar a couple of years ago -
       | https://github.com/adamisntdead/QuSimPy
       | 
       | Happy to answer any questions people have, including on other
       | simulation methods other than state vector!
        
         | reikonomusha wrote:
         | One of the aspects emphasized in TFA is being able to simulate
         | gates of any dimension/number of qubits (like a 3-qubit Toffoli
         | or a 5-qubit Molmer-Sorensen), instead of just 1- and 2-qubit
         | gates. Have you thought about extending your simulator to
         | support gates of greater than two qubits?
        
           | adamisntdead wrote:
           | I guess back when I wrote it I didn't see the need given that
           | single qubit gates along with CNOT are universal and they're
           | implemented. I think airing on the side of 'as few features
           | as is educational' was my mentality on this.
        
       | jramirezpr wrote:
       | On the X gate, the more relevant identity regarding X being its
       | own inverse is X*X=I, the other one is true for all invertible
       | matrices
        
       | frankdejonge wrote:
       | You had me until Lisp.
        
       | eggy wrote:
       | I'm deciding between Common Lisp and Mojo, so maybe I will try to
       | implement this in Mojo and compare. Anybody have any thoughts on
       | this, since I am a mediocre Lisper and a beginning Mojo person. I
       | am familiar with Numpy and I also program in APL and J.
        
       | mighmi wrote:
       | Any recommendations on a textbook to understand this kind of
       | math?
        
         | prezjordan wrote:
         | Might be of use! https://quantum.country/
        
           | reikonomusha wrote:
           | One of the authors, Michael Nielson, also wrote the "bible"
           | of quantum computing, as referenced in a sibling comment. The
           | remains one of the most popular and cited introductory texts
           | to the subject, though today there are many more options on
           | the market.
        
         | hackandthink wrote:
         | Ronald de Wolf: Quantum Computing: Lecture Notes
         | 
         | Ronald de Wolf:
         | 
         | https://arxiv.org/abs/1907.09415
         | 
         | David Bacon:
         | 
         | https://courses.cs.washington.edu/courses/cse599d/06wi/
         | 
         | Scott Aaronson:
         | 
         | https://en.wikipedia.org/wiki/Quantum_Computing_Since_Democr...
        
         | abdullahkhalids wrote:
         | Quantum Computation and Quantum Information by Nielson and
         | Chuang is the standard textbook for quantum computing. It has
         | excellent background chapters on linear algebra and quantum
         | mechanics, that will get you a lot of mileage.
         | 
         | Another book that I would recommend is Introduction to
         | Classical and Quantum Computing by Thomas Wong. It's recent and
         | has lots of examples, including lots of code, to show how to
         | work with the math.
        
         | jksk61 wrote:
         | maybe not of your interest, but if one is more from a ML
         | background, there's this nice book/overview by Maria Schuld and
         | Francesco Petruccione, called Machine Learning with Quantum
         | Computers, which is mostly regarding variational algorithm and
         | quantum kernel machine. However, the best intro material is the
         | one by Nielsen and Chuang.
         | 
         | Moreover, if you feel more comfortable by running examples
         | there are qiskit, pennylane and other libraries which shares a
         | lot of tutorials and notebook (and some can be run on a true
         | quantum computer if you have the money or the time to wait in
         | queue!)
        
       | Strilanc wrote:
       | The hardest part of implementing a quantum state vector
       | simulation is understanding how the tensor product expands an
       | n-qubit gate to apply to an m-qubit system. (A third of the
       | linked post is dedicated to it; appropriately.)
       | 
       | If you use a language or framework that's based on tensors to
       | start with, things can be quite succinct (though you still need
       | to understand the concepts). For example, in numpy, if you store
       | the state vector in an array of shape (2,) * num_qubits, you can
       | apply gates as one-liners using np.einsum:
       | import numpy as np              # Init 4-qubit system with all
       | amplitude in the 0000 state.         state = np.zeros(shape=(2,)
       | * 4, dtype=np.complex64)         state[(0,) * 4] = 1
       | # Unitary matrix of Hadamard gate         H = np.array([[1, 1],
       | [1, -1]], dtype=np.complex64) / 2**0.5              # Apply
       | Hadamard gate to third qubit of four qubit system.         state
       | = np.einsum('XY,abXd->abYd', H, state)
       | 
       | Here's a post explaining what np.einsum does:
       | https://obilaniu6266h16.wordpress.com/2016/02/04/einstein-su... .
       | In the above einsum string 'XY,abXd->abYd' the 'XY' part is
       | naming the input and output axes of the Hadamard matrix, and the
       | 'abXd->abYd' part is saying to multiply the matrix into the third
       | axis of the state tensor. The notation is pretty general, able to
       | permute and repeat axes in order to express things like traces
       | and transposes and dot products and etc.
        
         | reikonomusha wrote:
         | "einsum" in Lisp [1]. It uses S-expressions instead of strings,
         | and compiles to native loops.
         | 
         | [1] https://github.com/quil-lang/magicl/blob/master/src/high-
         | lev...
        
           | lionsdan wrote:
           | (Link didn't work for me)
           | 
           | https://github.com/quil-lang/magicl/blob/master/src/high-
           | lev...
        
         | jxf wrote:
         | > The hardest part of implementing a quantum state vector
         | simulation is understanding how the tensor product expands an
         | n-qubit gate to apply to an m-qubit system. (A third of the
         | linked post is dedicated to it; appropriately.)
         | 
         | This feels like it could be the "git gets easier once you
         | understand branches are homeomorphic endofunctors mapping
         | submanifolds of a Hilbert space" of physics.
        
       ___________________________________________________________________
       (page generated 2023-07-16 23:00 UTC)