A simple notebook showing how to build a quantum randum number generator. We're going to use Float32 for all calculations to save memory.
We define some well known constants. The 2x2 identity matrix and the 2x2 zero matrix:
using Test, LinearAlgebra
eye = Matrix{Float32}(I, 2, 2)
ZEROS = zeros(Float32, 2, 2)
@test eye*eye ≈ eye && eye * ZEROS == ZEROS
Test Passed
We'll define the starting state for qubits as |0>.
UP = [1f0, 0]
DOWN = [0f0, 1f0]
2-element Array{Float32,1}: 0.0 1.0
Now we can define some basic gates:
X = [0f0 1; 1 0]
H = Float32(1/sqrt(2)) * [1f0 1; 1 -1]
halfX = Float32(0.5) * [1f0+im 1-im; 1-im 1+im]
@test halfX*halfX ≈ X && H*H ≈ X*X && X*X ≈ eye
Test Passed
qubits(n) = foldr(kron, fill(UP, n))
qubits (generic function with 1 method)
circuit(n) = foldr(kron, fill(H, n))
circuit (generic function with 1 method)
register = circuit(8)*qubits(8)
256-element Array{Float32,1}: 0.062499993 0.062499993 0.062499993 0.062499993 0.062499993 0.062499993 0.062499993 0.062499993 0.062499993 0.062499993 0.062499993 0.062499993 0.062499993 ⋮ 0.062499993 0.062499993 0.062499993 0.062499993 0.062499993 0.062499993 0.062499993 0.062499993 0.062499993 0.062499993 0.062499993 0.062499993
M0 = FALSE * transpose(FALSE)
2×2 Array{Float32,2}: 1.0 0.0 0.0 0.0
rand() > 0.5
false
measure(i, n) = transpose(register)*foldr(kron, map(k -> k==i ? M0 : eye, [1:8]...))*register
measure (generic function with 1 method)
measureAll(n) = map(v -> measure(v, n), collect(1:8))
measureAll (generic function with 1 method)
measureAll(8)
8-element Array{Float32,1}: 0.49999988 0.49999988 0.49999988 0.49999994 0.49999994 0.49999994 0.49999994 0.49999994