/// Quantum computing library for integer 2D qubits
/ Intended for use with the book Quantum Coputing for Everyone, by Chris Bernhardt
/ Page and section references prefixed with ($) relate to this book, MIT Press paperback edition, 2020
/ Some ideas from the book have been extended (e.g., additional "bra versions of functions", CCY and CCZ gates, etc.)
/ Terminology has been extended to enable use with other books (e.g., plus/minus vectors as alternate names for left/right)
/// Instantiation
/ In linear algebra, vectors are 1xN (bra's) or Nx1 matrices (ket's).
/ In K we cannot represent this with K vectors because we wouldn't be able to differentiate them
/ and transposing wouldn't work as expected. We need an additinoal level of enlisting
/ Note that these "extra enlisments" will need to be undone at some points (e.g., for extracting scalar results)
/ ($) Page 19, Vectors
braV:{,x} / bra from K vector
ketV:{+,x} / ket from K vector
/// Basic Constant bras and kets
bra0:braup:braV[1 0.]
bra1:bradown:braV[0 1.]
ket0:ketup:ketV[1 0.] / ($) Page 26, Orthonormal Bases
ket1:ketdown:ketV[0 1.] / ($) Page 26, Orthonormal Bases
/// Utility matrices
I:{1.*(!x)=/:!x} / Identity Matrix of order x / ($) Page 121, The Gates I and Z
ZM:{x#,x#0.} / Zero Matrix of order x. Note that Z is the Z gate, that is why we are using ZM for the Zero Matrix. Beware of confusion when typing!
/// Rotations and corresponding constants
PI:2.*_acos[0] / Needed because K2 doesn't yet have _pi
rotatebra:{a:y*PI%180.;:_mul[x;((_cos[a];-_sin[a]);(_sin[a];_cos[a]))]} / 2D Cartesian or polarization rotation of a bra
rotateket:{a:y*PI%180.;:_mul[((_cos[a];-_sin[a]);(_sin[a];_cos[a]));x]} / 2D Cartesian or polarization rotation of a ket
rotatebraS:{a:y*PI%360.;:_mul[x;((_cos[a];-_sin[a]);(_sin[a];_cos[a]))]} / 2D spin rotation of a bra
rotateketS:{a:y*PI%360.;:_mul[((_cos[a];-_sin[a]);(_sin[a];_cos[a]));x]} / 2D spin rotation of a ket
braright:braminus:rotatebraS[bra0;-90.]
braleft:braplus:rotatebraS[bra1;-90.]
braur:rotatebraS[bra0;-60]
bradl:rotatebraS[bra1;-60]
ketright:ketminus:rotateketS[ket0;-90.] / ($) Page 26, Orthonormal Bases
ketleft:ketplus:rotateketS[ket1;-90.] / ($) Page 26, Orthonormal Bases
ketur:rotateketS[ket0;-60.] / ($) Page 26, Orthonormal Bases & Page 45, Rotating the apparatus through 60
ketdl:rotateketS[ket1;-60.] / ($) Page 26, Orthonormal Bases & Page 45, Rotating the apparatus through 60
/// Toolbox
braket:{**_mul[x;y]} / Multiplying a bra by a ket / ($) Page 23, Multiplying a Bra by a Ket
lenbra:{_sqrt[braket[x;+x]]}
lenket:{_sqrt[braket[+x;x]]} / ($) Page 20, Lengths of Vectors & Page 24, bra-kets and length
/ Scalar multiplication is handled by K's multiplication operator, dyadic * / ($) Page 21, Scalar multiplication
/ Vector addition is handled by K's addition operator, dyadic + / ($) Page 20, Lengths of Vectors
/ Bra to Ket conversions in both directions are handled by K's transpose operator, monadic + / ($) Pages 21-22 Vector Addition
areorthogonalbra:{0=braket[x;+y]}
areorthogonalket:{0=braket[+x;y]} / ($) Page 22, Orthogonal Vectors & Page 24-25 Bra-kets and Orthogonality
/// 2x2 Gates
X:((0 1.);(1 0.)) / Pauli X gate / ($) Page 122, The Gates X and Y
Y:((0 1.);(-1 0.)) / Pauli Y gate / ($) Page 122, The Gates X and Y
Z:((1 0.);(0 -1.)) / Pauli Z gate / ($) Page 121, The Gates I and Z
H:((1 1.);(1 -1.))%_sqrt 2 / Hadamard gate / ($) Page 122, The Hadamard Gate
/// 4x4 Gates
controlled:{(I[#x],'ZM[#x]),ZM[#x],'x}
CX:controlled X / Controlled X gate (CNOT) / ($) Page 118-120, The CNOT Gate
CY:controlled Y / Controlled Y gate
CZ:controlled Z / Controlled Z gate
CH:controlled H / Controlled Hadamard gate
SWAP:I[4][0 2 1 3] / SWAP gate
/// 8x8 Gates
CCX: controlled controlled X / Toffoli gate (CCNOT) / ($) Page 107-109, The Toffoli Gate
CCY: controlled controlled Y
CCZ: controlled controlled Z
/ Gate Application
ketX:_mul[X;] / Ket that results from applying the X gate to another ket
ketY:_mul[Y;] / Ket that results from applying the Y gate to another ket
ketZ:_mul[Z;] / Ket that results from applying the Z gate to another ket
ketH:_mul[H;] / Ket that results from applying the Hadamard gate to another ket