Live data from Hacker News

A tutorial quantum interpreter in 150 lines of Lisp

stylewarning.com

1–10 of 46 posts

Re: A tutorial quantum interpreter in 150 lines of Lisp

#3
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.

Re: A tutorial quantum interpreter in 150 lines of Lisp

#5
post #2

Any recommendations on a textbook to understand this kind of math?

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.

Re: A tutorial quantum interpreter in 150 lines of Lisp

#6
post #3

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…

"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...

Re: A tutorial quantum interpreter in 150 lines of Lisp

#7
post #2

Any recommendations on a textbook to understand this kind of math?

Might be of use! https://quantum.country/

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.

Re: A tutorial quantum interpreter in 150 lines of Lisp

#8
post #3

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…

"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...

(Link didn't work for me)

https://github.com/quil-lang/magicl/blob/master/src/high-lev...

Post reply on HN