A tutorial quantum interpreter in 150 lines of Lisp
stylewarning.com
A tutorial quantum interpreter in 150 lines of Lisp
1–10 of 46 posts
Re: A tutorial quantum interpreter in 150 lines of Lisp
#2Re: A tutorial quantum interpreter in 150 lines of Lisp
#3If 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
#4Any recommendations on a textbook to understand this kind of math?
Re: A tutorial quantum interpreter in 150 lines of Lisp
#5Any recommendations on a textbook to understand this kind of math?
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
#6The 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…
[1] https://github.com/quil-lang/magicl/blob/master/src/high-lev...
Re: A tutorial quantum interpreter in 150 lines of Lisp
#7Any recommendations on a textbook to understand this kind of math?
Might be of use! https://quantum.country/
Re: A tutorial quantum interpreter in 150 lines of Lisp
#8The 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...
https://github.com/quil-lang/magicl/blob/master/src/high-lev...
Re: A tutorial quantum interpreter in 150 lines of Lisp
#9Happy to answer any questions people have, including on other simulation methods other than state vector!