{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "A simple notebook showing how to build a quantum randum number generator. We're going to use Float32 for all calculations to save memory.\n", "\n", "We define some well known constants. The 2x2 identity matrix and the 2x2 zero matrix:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[32m\u001b[1mTest Passed\u001b[22m\u001b[39m" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using Test, LinearAlgebra\n", "\n", "eye = Matrix{Float32}(I, 2, 2)\n", "ZEROS = zeros(Float32, 2, 2)\n", "\n", "@test eye*eye ≈ eye && eye * ZEROS == ZEROS" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We'll define the starting state for qubits as |0>." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2-element Array{Float32,1}:\n", " 0.0\n", " 1.0" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "UP = [1f0, 0]\n", "DOWN = [0f0, 1f0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can define some basic gates:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\u001b[32m\u001b[1mTest Passed\u001b[22m\u001b[39m" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "X = [0f0 1; 1 0]\n", "H = Float32(1/sqrt(2)) * [1f0 1; 1 -1]\n", "halfX = Float32(0.5) * [1f0+im 1-im; 1-im 1+im]\n", "\n", "@test halfX*halfX ≈ X && H*H ≈ X*X && X*X ≈ eye" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "qubits (generic function with 1 method)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "qubits(n) = foldr(kron, fill(UP, n))" ] }, { "cell_type": "raw", "metadata": {}, "source": [ "%%latex\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$$\\frac{x}{y}$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "circuit (generic function with 1 method)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "circuit(n) = foldr(kron, fill(H, n))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "256-element Array{Float32,1}:\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " ⋮ \n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993\n", " 0.062499993" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "register = circuit(8)*qubits(8)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2×2 Array{Float32,2}:\n", " 1.0 0.0\n", " 0.0 0.0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "M0 = FALSE * transpose(FALSE)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "false" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rand() > 0.5" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "measure (generic function with 1 method)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "measure(i, n) = transpose(register)*foldr(kron, map(k -> k==i ? M0 : eye, [1:8]...))*register" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "measureAll (generic function with 1 method)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "measureAll(n) = map(v -> measure(v, n), collect(1:8))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8-element Array{Float32,1}:\n", " 0.49999988\n", " 0.49999988\n", " 0.49999988\n", " 0.49999994\n", " 0.49999994\n", " 0.49999994\n", " 0.49999994\n", " 0.49999994" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "measureAll(8)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Julia 1.1.0", "language": "julia", "name": "julia-1.1" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.1.0" } }, "nbformat": 4, "nbformat_minor": 2 }