Hi, my name is Craig

and I miss university.

REACT

QUANTUM

COMPUTING

QUANTUM

COMPUTING

Quantum State

Quantum Gates

Quantum Measurement

Quantum Algorithms

?

Wow, you really do just put the word Quantum in front of everything.

Unknown

QUANTUM

COMPUTING

Quantum State

Quantum Gates

Quantum Measurement

Quantum Algorithms

?

Wow, you really do just put the word Quantum in front of everything.

Unknown

QUANTUM

COMPUTING

Quantum State

Quantum Gates

Quantum Measurement

Quantum Algorithms

?

Wow, you really do just put the word Quantum in front of everything.

Unknown

Go to your computer, Craig.

Unknown

?

QUANTUM

COMPUTING

Quantum State

Quantum Gates

Quantum Measurement

Quantum Algorithms

?

Wow, you really do just put the word Quantum in front of everything.

Unknown

Go to your computer, Craig.

Unknown

?

QUANTUM

COMPUTING

Quantum State

Quantum Gates

Quantum Measurement

Quantum Algorithms

?

Wow, you really do just put the word Quantum in front of everything.

Unknown

Go to your computer, Craig.

Unknown

?

QUANTUM

COMPUTING

Quantum State

Quantum Gates

Quantum Measurement

Quantum Algorithms

?

Wow, you really do just put the word Quantum in front of everything.

Unknown

Go to your computer, Craig.

Unknown

?

HI CRAIG IT'S

?

It's Scott, Ant-Man, whatever, hey I need your help. 🐜

Unknown

?

It's Scott, Ant-Man, whatever, hey I need your help. 🐜

Unknown

Hurry up it's important. 🐜

Scott Lang

HI CRAIG IT'S

?

It's Scott, Ant-Man, whatever, hey I need your help. 🐜

Unknown

Hurry up it's import🐜. 🐜

Scott Lang

HI CRAIG IT'S

?

It's Scott, Ant-Man, whatever, hey I need your help. 🐜

Unknown

Hurry up it's import🐜. 🐜

Scott Lang

HI CRAIG IT'S

?

It's Scott, Ant-Man, whatever, hey I need your help. 🐜

Unknown

Hurry up it's import🐜. 🐜

Scott Lang

I am in the middle of a talk.

Reply:

HI CRAIG IT'S

?

It's Scott, Ant-Man, whatever, hey I need your help. 🐜

Unknown

Hurry up it's import🐜. 🐜

Scott Lang

I am in the middle of a talk.

Reply:

HI CRAIG IT'S

?

It's Scott, Ant-Man, whatever, hey I need your help. 🐜

Unknown

Hurry up it's import🐜. 🐜

Scott Lang

I am in the middle of a talk.

Reply:

HI CRAIG IT'S

?

It's Scott, Ant-Man, whatever, hey I need your help. 🐜

Unknown

Hurry up it's important. 🐜

Scott Lang

I am in the middle of a talk.

Reply:

HI CRAIG IT'S

I KNOW! That's why I need you. Teach the nice people about Qubits and I'll be in touch. 🐜

Scott Lang

?

It's Scott, Ant-Man, whatever, hey I need your help. 🐜

Unknown

HI CRAIG IT'S

I KNOW! That's why I need you. Teach the nice people about Qubits and I'll be in touch. 🐜

Scott Lang

?

It's Scott, Ant-Man, whatever, hey I need your help. 🐜

Unknown

I KNOW! That's why I need you. Teach the nice people about Qubits and I'll be in touch. 🐜

Scott Lang

HI CRAIG IT'S

?

It's Scott, Ant-Man, whatever, hey I need your help. 🐜

Unknown

OH AND I'M IN YOUR JAVASCRIPT
🐜🐜🐜🐜🐜🐜🐜🐜🐜🐜
🐜🐜🐜🐜🐜🐜🐜🐜🐜🐜
🐜🐜🐜🐜🐜🐜🐜🐜🐜🐜
🐜🐜🐜🐜🐜🐜🐜🐜🐜🐜
🐜🐜🐜🐜🐜🐜🐜🐜🐜🐜
🐜🐜🐜🐜🐜🐜🐜🐜🐜🐜

Bits:

0

A representation of a binary state

Qubits:

|\psi\rangle = \alpha |0\rangle + \beta |1\rangle \quad \text{with} \quad |\alpha|^2 + |\beta|^2 = 1

A representation of a quantum state

10
0110
11010110
0011101011010110
00101111100101100011101011010110
0010111110010110001110101101011000101111100101100011101011010110
\begin{bmatrix} \alpha \ \beta \end{bmatrix}
\begin{bmatrix} \gamma \ \delta \ \alpha \ \beta \end{bmatrix}
\begin{bmatrix} \epsilon \ \zeta \ \eta \ \theta \ \gamma \ \delta \ \alpha \ \beta \end{bmatrix}
\begin{bmatrix} \iota \ \kappa \ \lambda \ \mu \ \nu \ \xi \ \omicron \ \pi \ \epsilon \ \zeta \ \eta \ \theta \ \gamma \ \delta \ \alpha \ \beta \end{bmatrix}

Quantum State

Qubits:

|\psi\rangle = \alpha |0\rangle + \beta |1\rangle \quad \text{with} \quad |\alpha|^2 + |\beta|^2 = 1

A representation of a quantum state

Quantum State

Qubits:

|\psi\rangle = \alpha |0\rangle + \beta |1\rangle \quad \text{with} \quad |\alpha|^2 + |\beta|^2 = 1

A representation of a quantum state

\alpha
\alpha
\beta
\beta
(1, 0)
(0, 0)

Quantum State

Qubits:

|\psi\rangle = \alpha |0\rangle + \beta |1\rangle \quad \text{with} \quad |\alpha|^2 + |\beta|^2 = 1

A representation of a quantum state

\alpha
\alpha
\beta
\beta
1 + 0i
0 + 0i

Quantum State

Qubits:

|\psi\rangle = \alpha |0\rangle + \beta |1\rangle \quad \text{with} \quad |\alpha|^2 + |\beta|^2 = 1

A representation of a quantum state

\alpha
\alpha
\beta
\beta
\quad | \ \ \ \ \ \ \ \ \ \ \ |^2 + | \ \ \ \ \ \ \ \ \ \ \ |^2 = 1
1 + 0i
0 + 0i

Quantum State

Qubits:

|\psi\rangle = \alpha |0\rangle + \beta |1\rangle \quad \text{with} \quad |\alpha|^2 + |\beta|^2 = 1

A representation of a quantum state

\alpha
\alpha
\beta
\beta
\quad | \ \ |^2 + | \ \ |^2 = 1
1
0

Quantum State

Qubits:

|\psi\rangle = \alpha |0\rangle + \beta |1\rangle \quad \text{with} \quad |\alpha|^2 + |\beta|^2 = 1

A representation of a quantum state

\alpha
\alpha
\beta
\beta
(0, 0)
(0, -i)

Quantum State

Qubits:

|\psi\rangle = \alpha |0\rangle + \beta |1\rangle \quad \text{with} \quad |\alpha|^2 + |\beta|^2 = 1

A representation of a quantum state

\alpha
\alpha
\beta
\beta
0 + 0i
0 - i

Quantum State

Qubits:

|\psi\rangle = \alpha |0\rangle + \beta |1\rangle \quad \text{with} \quad |\alpha|^2 + |\beta|^2 = 1

A representation of a quantum state

\alpha
\alpha
\beta
\beta
\quad | \ \ \ \ \ \ \ \ \ \ \ |^2 + | \ \ \ \ \ \ \ \ \ \ \ |^2 = 1
0 + 0i
0 - i

Quantum State

Qubits:

|\psi\rangle = \alpha |0\rangle + \beta |1\rangle \quad \text{with} \quad |\alpha|^2 + |\beta|^2 = 1

A representation of a quantum state

\alpha
\alpha
\beta
\beta
\quad | \ \ |^2 + | \ \ |^2 = 1
0
1

Quantum State

Qubits:

|\psi\rangle = \alpha |0\rangle + \beta |1\rangle \quad \text{with} \quad |\alpha|^2 + |\beta|^2 = 1

A representation of a quantum state

\alpha
\alpha
\beta
\beta
\quad | \ \ \ \ \ \ \ \ \ |^2 + | \ \ \ \ \ \ \ \ \ |^2 = 1
\frac{1}{\sqrt{2}}
\frac{i}{\sqrt{2}}

Quantum State

Qubits:

|\psi\rangle = \alpha |0\rangle + \beta |1\rangle \quad \text{with} \quad |\alpha|^2 + |\beta|^2 = 1

A representation of a quantum state

\alpha
\alpha
\beta
\beta
\ \ \ \ \ + \ \ \ \ \ \ = 1
0.5
0.5

Quantum State

Quantum State

const psi = [
  probabilityA, 
  probabilityB
];

Qubits:

A representation of a quantum state

const psi = [
	complex(1 / Math.sqrt(2), 0), // { r: 0.7071067812, i: 0 }
    complex(0, 1 / Math.sqrt(2))  // { r: 0, i: 0.7071067812 }
];
|\psi\rangle = \alpha |0\rangle + \beta |1\rangle \quad \text{with} \quad |\alpha|^2 + |\beta|^2 = 1
\alpha
\alpha
\beta
\beta
\begin{bmatrix} \alpha \\ \beta \end{bmatrix}

Quantum State

Qubits:

A representation of a quantum state

Oh cool, maths. Okay here's the deal, I'm locked up in the Quantum Realm, and I need you to help me get out. 🐜

Scott Lang

type Complex = [number, number];
type Row = Array<Complex>;
type Matrix = Array<Row>;

function complex (r = 0, i = 0): Complex {
  return [r, i];
}

function kZero(): Matrix {
  return [[complex(1)], [complex(0)]];
}

function kOne(): Matrix {
  return [[complex(0)], [complex(1)]];
}

Quantum State

Qubits:

A representation of a quantum state

Oh cool, maths. Okay here's the deal, I'm locked up in the Quantum Realm, and I need you to help me get out. 🐜

Scott Lang

type Complex = [number, number];
type Row = Array<Complex>;
type Matrix = Array<Row>;

function complex (r = 0, i = 0): Complex {
  return [r, i];
}

function kZero(): Matrix {
  return [[complex(1)], [complex(0)]];
}

function kOne(): Matrix {
  return [[complex(0)], [complex(1)]];
}

?

It's Scott, Ant-Man, whatever, hey I need your help. 🐜

Unknown

Someone knocked me out, and I woke up in a prison cell. 🐜

Scott Lang

Someone knocked me out, and I woke up in a prison cell. 🐜

Scott Lang

BUT I accessed the system that controls the locks, and entangled myself with your CPU...

Scott Lang

Someone knocked me out, and I woke up in a prison cell. 🐜

Scott Lang

BUT I accessed the system that controls the locks, and entangled myself with your CPU...

Scott Lang

So I can send you messages and run code. 🐜

Scott Lang

Someone knocked me out, and I woke up in a prison cell. 🐜

Scott Lang

BUT I accessed the system that controls the locks, and entangled myself with your CPU...

Scott Lang

So I can send you messages and run code. 🐜

Scott Lang

Someone knocked me out, and I woke up in a prison cell. 🐜

Scott Lang

BUT I accessed the system that controls the locks, and entangled myself with your CPU...

Scott Lang

So I can send you messages and run code. 🐜

Scott Lang

Someone knocked me out, and I woke up in a prison cell. 🐜

Scott Lang

BUT I accessed the system that controls the locks, and entangled myself with your CPU...

Scott Lang

So I can send you messages and run code. 🐜

Scott Lang

Someone knocked me out, and I woke up in a prison cell. 🐜

Scott Lang

So I can send you messages and run code. 🐜

Scott Lang

Shit, someone's coming, one sec. 🐜

Scott Lang

Someone knocked me out, and I woke up in a prison cell. 🐜

Scott Lang

So I can send you messages and run code. 🐜

Scott Lang

Shit, someone's coming, one sec. 🐜

Scott Lang

Quantum Measurement

const bit: boolean = false;
console.log(bit); // false

Quantum Measurement

function normalise(ket: Matrix): KeMatrixt {
  const flat = ket.map(([c]) => c);
  const total = flat.reduce((sum,[re,im])=>sum+re*re+im*im,0);
  return flat.map(([re,im])=>[re/Math.sqrt(total), im/Math.sqrt(total)]);
}

function measure(ket: Matrix): Matrix {
  const normalised = normalise(ket);

  const probabilities = normalised.map(([re, im]) => re*re + im*im);
  const cumulative = [];
  probabilities.reduce((acc, p, i) => {
    cumulative[i] = acc + p;
    return acc + p;
  }, 0);

  const r = Math.random();
  for (let i = 0; i < cumulative.length; i++) {
    if (r < cumulative[i]) return i;
  }

  return cumulative.length - 1;
}

Quantum Measurement

\lvert \psi \rangle = 1 \lvert 0 \rangle + 0 \lvert 1 \rangle
const psi = [
	[complex(1, 0)],
    [complex(0, 0)]
];
\quad |\alpha|^2 + |\beta|^2 = 1
\alpha
\beta
\ \ \ \ \ + \ \ \ \ \ \ = 1
1
0
measure(psi); // 0
measure(psi); // 0
measure(psi); // 0
measure(psi); // 0
|\psi\rangle = \begin{bmatrix} 1 \\ 0 \\ \end{bmatrix}

Quantum Measurement

\lvert \psi \rangle = \tfrac{1}{\sqrt2} \lvert 0 \rangle + \tfrac{i}{\sqrt2} \lvert 1 \rangle
const psi = [
	[complex(1 / Math.sqrt(2), 0)],
    [complex(0, 1 / Math.sqrt(2))]
];
measure(psi); // 0
measure(psi); // 1
measure(psi); // 0
measure(psi); // 0
measure(psi); // 1
measure(psi); // 1
\quad |\alpha|^2 + |\beta|^2 = 1
\alpha
\beta
\ \ \ \ \ + \ \ \ \ \ \ = 1
0.5
0.5
|\psi\rangle = \begin{bmatrix} \tfrac{1}{\sqrt2} \\ \tfrac{i}{\sqrt2} \end{bmatrix}

Quantum Measurement

|\psi\rangle = \tfrac{1}{\sqrt{2}} \left( |00\rangle + |11\rangle \right)
const psi = [
	[complex(1 / Math.sqrt(2), 0)],
	[complex(0, 0)],
	[complex(0, 0)],
    [complex(1 / Math.sqrt(2), 0)]
];

CRAIG. 🐜

Scott Lang

|\psi\rangle = \begin{bmatrix} \tfrac{1}{\sqrt{2}} \\ 0 \\ 0 \\ \tfrac{1}{\sqrt{2}} \end{bmatrix}
\quad | \ \ |^2 + | \ \ |^2 + | \ \ |^2 + | \ \ |^2 = 1
a
b
c
d
\ \ \ \ \ + \ \ \ \ \ + \ \ \ \ \ + \ \ \ \ \ \ = 1
0.5
0.5
0
0
measure(psi); // 00
measure(psi); // 11
measure(psi); // 11
measure(psi); // 00

Quantum Measurement

|\psi\rangle = 0|00\rangle + 0|01\rangle + \tfrac{1}{\sqrt{2}}|10\rangle + \tfrac{1}{\sqrt{2}}|11\rangle
const psi = [
	[complex(0, 0)],
	[complex(0, 0)],
	[complex(1 / Math.sqrt(2), 0)],
    [complex(1 / Math.sqrt(2), 0)]
];

CRAIG. 🐜

Scott Lang

|\psi\rangle = \begin{bmatrix} 0 \\ 0 \\ \tfrac{1}{\sqrt{2}} \\ \tfrac{1}{\sqrt{2}} \end{bmatrix}
measure(ignore(psi, 1)); // 0
measure(ignore(psi, 1)); // 0
measure(ignore(psi, 1)); // 0
measure(ignore(psi, 1)); // 0

Sometimes we only care about a specific qubit:

Quantum Measurement

CRAIG. 🐜

Scott Lang

function ignore(ket: Matrix, n: number): Matrix {
    const normalized = normalise(ket);
    const size = normalized.length;
    const chunkSize = Math.pow(2, n);
    const newKet: Ket = [];

    for (let i = 0; i < size / chunkSize; i++) {
        let sumProb = 0;
        for (let p = i * chunkSize; p < (i + 1) * chunkSize; p++) {
            const [re, im] = normalized[p];
            sumProb += re * re + im * im;
        }
        newKet.push([[Math.sqrt(sumProb), 0]]);
    }
    return newKet;
}

Quantum Measurement

CRAIG. 🐜

Scott Lang

|\psi\rangle = \tfrac{1}{\sqrt{2}} \left( |00\rangle + |11\rangle \right)
|\psi\rangle = \begin{bmatrix} \tfrac{1}{\sqrt{2}} \\ 0 \\ 0 \\ \tfrac{1}{\sqrt{2}} \end{bmatrix}
\quad | \ \ |^2 + | \ \ |^2 + | \ \ |^2 + | \ \ |^2 = 1
a
b
c
d
\ \ \ \ \ + \ \ \ \ \ + \ \ \ \ \ + \ \ \ \ \ \ = 1
0.5
0.5
0
0
measure(psi); // 00
measure(psi); // 11
measure(psi); // 11
measure(psi); // 00

CRAIG. 🐜

Scott Lang

const psi = [
	[complex(1 / Math.sqrt(2), 0)],
	[complex(0, 0)],
	[complex(0, 0)],
    [complex(1 / Math.sqrt(2), 0)]
];

Quantum Measurement

CRAIG. 🐜

Scott Lang

CRAIG. 🐜

Scott Lang

CRAIG. 🐜

Scott Lang

|\psi\rangle = \tfrac{1}{\sqrt{2}} \left( |00\rangle + |11\rangle \right)
|\psi\rangle = \begin{bmatrix} \tfrac{1}{\sqrt{2}} \\ 0 \\ 0 \\ \tfrac{1}{\sqrt{2}} \end{bmatrix}
\quad | \ \ |^2 + | \ \ |^2 + | \ \ |^2 + | \ \ |^2 = 1
a
b
c
d
\ \ \ \ \ + \ \ \ \ \ + \ \ \ \ \ + \ \ \ \ \ \ = 1
0.5
0.5
0
0
measure(psi); // 00
measure(psi); // 11
measure(psi); // 11
measure(psi); // 00
const psi = [
	[complex(1 / Math.sqrt(2), 0)],
	[complex(0, 0)],
	[complex(0, 0)],
    [complex(1 / Math.sqrt(2), 0)]
];

Quantum Measurement

CRAIG. 🐜

Scott Lang

CRAIG. 🐜

Scott Lang

|\psi\rangle = \tfrac{1}{\sqrt{2}} \left( |00\rangle + |11\rangle \right)
|\psi\rangle = \begin{bmatrix} \tfrac{1}{\sqrt{2}} \\ 0 \\ 0 \\ \tfrac{1}{\sqrt{2}} \end{bmatrix}
\quad | \ \ |^2 + | \ \ |^2 + | \ \ |^2 + | \ \ |^2 = 1
a
b
c
d
\ \ \ \ \ + \ \ \ \ \ + \ \ \ \ \ + \ \ \ \ \ \ = 1
0.5
0.5
0
0
measure(psi); // 00
measure(psi); // 11
measure(psi); // 11
measure(psi); // 00

CRAIG. 🐜

Scott Lang

const psi = [
	[complex(1 / Math.sqrt(2), 0)],
	[complex(0, 0)],
	[complex(0, 0)],
    [complex(1 / Math.sqrt(2), 0)]
];
QUANTUM SECURITY CAM_1

I've made some progress! I've got access to the Quantum security system computer! 🐜

Scott Lang

CRAIG. 🐜

Scott Lang

CRAIG. 🐜

Scott Lang

CRAIG. 🐜

Scott Lang

QUANTUM SECURITY CAM_1

I've made some progress! I've got access to the Quantum security system computer! 🐜

Scott Lang

Darn it, there's three layers of security to get this door open. 🐜

Scott Lang

Darn it, there's three layers of security to get this door open. 🐜

Scott Lang

I'm going to try something quickly. 🐜

Scott Lang

Darn it, there's three layers of security to get this door open. 🐜

Scott Lang

I'm going to try something quickly. 🐜

Scott Lang

QUANTUM SECURITY CAM_1

Quantum Gates

Classical:

\begin{aligned} 00 &\mapsto 0 \\ 01 &\mapsto 0 \\ 10 &\mapsto 0 \\ 11 &\mapsto 1 \end{aligned}

AND:

OR:

\begin{aligned} 00 &\mapsto 0 \\ 01 &\mapsto 1 \\ 10 &\mapsto 1 \\ 11 &\mapsto 1 \end{aligned}

XOR:

\begin{aligned} 00 &\mapsto 0 \\ 01 &\mapsto 1 \\ 10 &\mapsto 1 \\ 11 &\mapsto 0 \end{aligned}

NAND:

\begin{aligned} 00 &\mapsto 1 \\ 01 &\mapsto 1 \\ 10 &\mapsto 1 \\ 11 &\mapsto 0 \end{aligned}

Apply a binary operation to a binary state:

Quantum Gates

Quantum:

Apply a matrix operation to a quantum state:

IDENTITY:

I = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}

HADAMARD:

H = \begin{bmatrix} \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\ \frac{1}{\sqrt{2}} & \frac{-1}{\sqrt{2}} \end{bmatrix}

Quantum Gates

Quantum:

I|\psi\rangle = \begin{bmatrix}1 & 0 \\ 0 & 1\end{bmatrix} \begin{bmatrix}1 \\ 0\end{bmatrix} = \begin{bmatrix}1\cdot 1 + 0 \cdot 0 \\ 0 1 + 1 \cdot 0\end{bmatrix}
= \begin{bmatrix}1 \\ 0\end{bmatrix} = |0\rangle + |1\rangle
\lvert \psi \rangle = \lvert 0 \rangle = \begin{bmatrix} 1 \\ 0 \\ \end{bmatrix}
I = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}

Apply a matrix operation to a quantum state:

Quantum Gates

Quantum:

Apply a matrix operation to a quantum state:

IDENTITY:

I = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}

HADAMARD:

H = \begin{bmatrix} \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\ \frac{1}{\sqrt{2}} & \frac{-1}{\sqrt{2}} \end{bmatrix}

Quantum Gates

Quantum:

Apply a matrix operation to a quantum state:

function mIdentity (n: number = 1): Matrix {
    const size = Math.pow(2, n);
    return Array.from({ length: size }, (_, row) =>
      Array.from({ length: size }, (_, column) => complex(row === column ? 1 : 0))
    );
}

function mHadamard(): Matrix {
    const oneOverRoot2 = 1 / Math.sqrt(2);
    return [
        [complex(oneOverRoot2), complex(oneOverRoot2)],
        [complex(oneOverRoot2), complex(-oneOverRoot2)],
    ];
}

Quantum Gates

Quantum:

H = \begin{bmatrix} \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\ \frac{1}{\sqrt{2}} & \frac{-1}{\sqrt{2}} \end{bmatrix}
H|\psi\rangle = \begin{bmatrix}\frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\ \frac{1}{\sqrt{2}} & \frac{-1}{\sqrt{2}}\end{bmatrix} \begin{bmatrix}1 \\ 0\end{bmatrix} = \begin{bmatrix}\frac{1}{\sqrt{2}}\cdot 1 + \frac{1}{\sqrt{2}} \cdot 0 \\ \frac{1}{\sqrt{2}}\cdot 1 + \frac{-1}{\sqrt{2}} \cdot 0\end{bmatrix}
= \begin{bmatrix}\frac{1}{\sqrt{2}} \\ \frac{1}{\sqrt{2}}\end{bmatrix} = \frac{|0\rangle + |1\rangle}{\sqrt{2}}
\lvert \psi \rangle = \lvert 0 \rangle = \begin{bmatrix} 1 \\ 0 \\ \end{bmatrix}

Quantum Gates

Quantum:

H = \begin{bmatrix} \frac{1}{\sqrt{2}} & \frac{1}{\sqrt{2}} \\ \frac{1}{\sqrt{2}} & \frac{-1}{\sqrt{2}} \end{bmatrix}
H|\psi\rangle = \begin{bmatrix}\frac{1}{\sqrt{2}}\cdot 1 + \frac{1}{\sqrt{2}} \cdot 0 \\ \frac{1}{\sqrt{2}}\cdot 1 + \frac{-1}{\sqrt{2}} \cdot 0\end{bmatrix}
H|\psi\rangle =\begin{bmatrix}\frac{1}{\sqrt{2}} \\ \frac{1}{\sqrt{2}}\end{bmatrix}
\lvert \psi \rangle = \lvert 0 \rangle = \begin{bmatrix} 1 \\ 0 \\ \end{bmatrix}
const H = mHadamard()
const psi = kZero();
const Hpsi = [
    [H[0][0] * psi[0] + H[0][1] * psi[1]],
    [H[1][0] * psi[0] + H[1][1] * psi[1]]
];
console.log(Hpsi); 
// [[0.7071067811865475], [0.7071067811865475]]

Quantum Gates

Quantum:

=\begin{bmatrix} \frac{1}{2} & \frac{1}{2} & \frac{1}{2} & \frac{1}{2} \\ \frac{1}{2} & \frac{-1}{2} & \frac{1}{2} & \frac{-1}{2} \\ \frac{1}{2} & \frac{1}{2} & \frac{-1}{2} & \frac{-1}{2} \\ \frac{1}{2} & \frac{-1}{2} & \frac{-1}{2} & \frac{1}{2} \end{bmatrix} \begin{bmatrix}0\\1\\0\\0\end{bmatrix}

As the number of qubits increases, so does the complexity of the expressions:

Kronecker Product

(H \otimes H)
\begin{bmatrix}0\\1\\0\\0\end{bmatrix}

Quantum Gates

Quantum:

Kronecker Product

(H \otimes H)
\begin{bmatrix}0\\1\\0\\0\end{bmatrix}

Quantum Gates

Quantum:

Kronecker Product

(H \otimes H)
\begin{bmatrix}0\\1\\0\\0\end{bmatrix}

Quantum Gates

Quantum:

Kronecker Product

\begin{bmatrix}0\\1\\0\\0\end{bmatrix}

Quantum Gates

Quantum:

Kronecker Product

= |0\rangle \otimes |1\rangle
\begin{bmatrix}0\\1\\0\\0\end{bmatrix}

Quantum Gates

Quantum:

Kronecker Product

= |0\rangle \otimes |1\rangle
\begin{bmatrix}0\\1\\0\\0\end{bmatrix}
= \begin{bmatrix}1\\0\end{bmatrix} \otimes \begin{bmatrix}0\\1\end{bmatrix}

Quantum Gates

Quantum:

Kronecker Product

\begin{bmatrix}1\\0\end{bmatrix} \otimes \begin{bmatrix}0\\1\end{bmatrix} =
const zero = kZero(); // [[1], [0]]
const one = kOne(); // [[0], [1]]

const zeroKronOne = [
  [zero[0] * one[0]],
  [zero[0] * one[1]],
  [zero[1] * one[0]],
  [zero[1] * one[1]]
];

console.log(zeroKronOne); // [[0], [1], [0], [0]]

Quantum Gates

Quantum:

Kronecker Product

const H = mHadamard();

const HkronH = [
  [
    H[0][0] * H[0][0], H[0][0] * H[0][1],
    H[0][1] * H[0][0], H[0][1] * H[0][1]
  ],
  [
    H[0][0] * H[1][0], H[0][0] * H[1][1],
    H[0][1] * H[1][0], H[0][1] * H[1][1]
  ],
  [
    H[1][0] * H[0][0], H[1][0] * H[0][1],
    H[1][1] * H[0][0], H[1][1] * H[0][1]
  ],
  [
    H[1][0] * H[1][0], H[1][0] * H[1][1],
    H[1][1] * H[1][0], H[1][1] * H[1][1]
  ]
];
H \otimes H =

Quantum Gates

Quantum:

Kronecker Product

const H = mHadamard();

const HkronH = [
  [
    H[0][0] * H[0][0], H[0][0] * H[0][1],
    H[0][1] * H[0][0], H[0][1] * H[0][1]
  ],
  [
    H[0][0] * H[1][0], H[0][0] * H[1][1],
    H[0][1] * H[1][0], H[0][1] * H[1][1]
  ],
  [
    H[1][0] * H[0][0], H[1][0] * H[0][1],
    H[1][1] * H[0][0], H[1][1] * H[0][1]
  ],
  [
    H[1][0] * H[1][0], H[1][0] * H[1][1],
    H[1][1] * H[1][0], H[1][1] * H[1][1]
  ]
];
(H \otimes H) =
function cAdd(c1: Complex, c2: Complex): Complex {
    const [c1r, c1i] = c1;
	const [c2r, c2i] = c2;
  	return [c1r + c2r, c1i + c2i];;
}

function cMultiply(c1: Complex, c2: Complex): Complex {
    const [c1r, c1i] = c1;
	const [c2r, c2i] = c2;
  	return [
      c1r * c2r - c1i * c2i, 
      c1i * c2r + c1r * c2i,
    ];
}

function mKron(...matrices: Array<Matrix>): Matrix {
    if (matrices.length === 1) {
      return matrices[0];
    }

    const [m1, m2, ...rest] = matrices;
    const height1 = m1.length, width1 = m1[0].length;
    const height2 = m2.length, width2 = m2[0].length;
    const kronProd: Matrix = Array.from(
      { length: height1 * height2 },
      () => Array(width1 * width2)
    );
    for (let m = 0; m < height1; m++) {
      for (let n = 0; n < width1; n++) {
        for (let p = 0; p < height2; p++) {
          for (let q = 0; q < width2; q++) {
            const newM = m * height2 + p;
            const newN = n * width2 + q;
            kronProd[newM][newN] = cMultiply(m1[m][n], m2[p][q]);
          }
        }
      }
    }
    return mKron(kronProd, ...rest);
}

function mMultiply (...matrices: Array<Matrix>): Matrix {
    const [first, second, ...rest] = matrices;
    const height1 = first.length,
      width1 = first[0].length;
    const height2 = second.length,
      width2 = second[0].length;

    if (width1 !== height2) {
      throw new Error("Matrix dimensions incorrect for multiplication.");
    }
    for (let r = 0; r < height1; r++) {
      for (let c = 0; c < width2; c++) {
        let val = complex(0);
        for (let k = 0; k < width1; k++) {
          val = cAdd(val, cMultiply(first[r][k], second[k][c]));
        }
        result[r][c] = val;
      }
    }
    let product = result;
    for (const m of rest) {
      product = mMultiply(product, m);
    }
    return product;
}
QUANTUM SECURITY CAM_1

OH HI CRAIG IT'S DARREN. I HAVE SCOTT.

MODOK

QUANTUM SECURITY CAM_1

OH HI CRAIG IT'S DARREN. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

QUANTUM SECURITY CAM_1

OH HI CRAIG IT'S DARREN. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

QUANTUM SECURITY CAM_1

OH HI CRAIG IT'S DARREN/MODOK. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

QUANTUM SECURITY CAM_1

OH HI CRAIG IT'S DARREN/MODOK. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

OH HI CRAIG IT'S DARREN/MODOK. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

Craig! It's Cassie, Scott's daughter!

Cassie Lang

OH HI CRAIG IT'S DARREN/MODOK. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

Craig! It's Cassie, Scott's daughter!

Cassie Lang

We've figured out how to open dad's cell! You need to run some Quantum Algorithms!

Cassie Lang

OH HI CRAIG IT'S DARREN/MODOK. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

Craig! It's Cassie, Scott's daughter!

Cassie Lang

We've figured out how to open dad's cell! You need to run some Quantum Algorithms!

Cassie Lang

It's Deutsch-Josza, Grover, Shor, in that order! But we don't know the inputs!?

Cassie Lang

OH HI CRAIG IT'S DARREN/MODOK. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

Craig! It's Cassie, Scott's daughter!

Cassie Lang

We've figured out how to open dad's cell! You need to run some Quantum Algorithms!

Cassie Lang

It's Deutsch-Josza, Grover, Shor, in that order! But we don't know the inputs!?

Cassie Lang

OH HI CRAIG IT'S DARREN/MODOK. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

Craig! It's Cassie, Scott's daughter!

Cassie Lang

We've figured out how to open dad's cell! You need to run some Quantum Algorithms!

Cassie Lang

It's Deutsch-Josza, Grover, Shor, in that order! But we don't know the inputs!?

Cassie Lang

Quantum Algorithms

Deutsch

The Deutsch Algorithm solves a problem that isn't particularly difficult or particularly important, but it can be solved faster on a quantum computer than on a classical computer.

f: \{0,1\} \to \{0,1\}
Constant = f(0) = f(1) \\ Balanced = f(0) \neq f(1)

Quantum Algorithms

Deutsch

const constant0 = () => 0
const constant1 = () => 1
const balancedX = (x) => x;
const balancedNotX = (x) => 1 - x;

const oracles = [constant0, constant1, balancedX, balancedNotX];
const oracle = oracles[Math.floor(Math.random() * oracles.length)];
f: \{0,1\} \to \{0,1\}
Constant = f(0) = f(1) \\ Balanced = f(0) \neq f(1)

Quantum Algorithms

Deutsch: Classical

const constant0 = () => 0
const constant1 = () => 1
const balancedX = (x) => x;
const balancedNotX = (x) => 1 - x;

const oracles = [constant0, constant1, balancedX, balancedNotX];
const oracle = oracles[Math.floor(Math.random() * oracles.length)];

const constant = oracle(0) === oracle(1);
const balanced = !constant;

A classical solution of Deutsch requires inspecting the oracle twice - once with each input

Quantum Algorithms

Deutsch: Quantum

|\psi_0\rangle = |0\rangle \otimes |1\rangle = \begin{bmatrix}0 \\ 1 \\ 0 \\ 0\end{bmatrix}

Initial state with two qubits

Quantum Algorithms

Deutsch: Quantum

H \otimes H = \begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & -1 & 1 & -1 \\ 1 & 1 & -1 & -1 \\ 1 & -1 & -1 & 1 \end{bmatrix}
|\psi_1\rangle = (H \otimes H)|\psi_0\rangle = \begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & -1 & 1 & -1 \\ 1 & 1 & -1 & -1 \\ 1 & -1 & -1 & 1 \end{bmatrix} \begin{bmatrix}0 \\ 1 \\ 0 \\ 0\end{bmatrix}

Hadamard gate for two qubits

Apply Hadamard gate to get superposition

Quantum Algorithms

Deutsch: Quantum

|\psi_2\rangle = U_f |\psi_1\rangle

Black Box "Oracle" for the unknown function

U_f = \begin{bmatrix} ? & ? & ? & ? \\ ? & ? & ? & ? \\ ? & ? & ? & ? \\ ? & ? & ? & ? \end{bmatrix}

Apply the Oracle to the superpositioned state

Quantum Algorithms

Deutsch: Quantum

H \otimes I = \begin{bmatrix} 1 & 0 & 1 & 0 \\ 0 & 1 & 0 & 1 \\ 1 & 0 & -1 & 0 \\ 0 & 1 & 0 & -1 \end{bmatrix}
|\psi_3\rangle = (H \otimes I) |\psi_2\rangle

First Qubit only Hadamard gate 

Apply the gate to the state

Quantum Algorithms

Deutsch: Quantum

|0\rangle \Rightarrow \text{function is constant} \\ |1\rangle \Rightarrow \text{function is balanced}
\text{Measure the first Qubit of } |\psi_3\rangle

The second qubit contains redundant information

Quantum Algorithms

Deutsch: Quantum

const constant0 = [[complex(1),complex(0),complex(0),complex(0)],[complex(0),complex(1),complex(0),complex(0)],[complex(0),complex(0),complex(1),complex(0)],[complex(0),complex(0),complex(0),complex(1)]];
const constant1 = [[complex(0),complex(1),complex(0),complex(0)],[complex(1),complex(0),complex(0),complex(0)],[complex(0),complex(0),complex(0),complex(1)],[complex(0),complex(0),complex(1),complex(0)]];
const balancedX = [[complex(1),complex(0),complex(0),complex(0)],[complex(0),complex(1),complex(0),complex(0)],[complex(0),complex(0),complex(0),complex(1)],[complex(0),complex(0),complex(1),complex(0)]];
const balancedNotX = [[complex(0),complex(0),complex(1),complex(0)],[complex(0),complex(0),complex(0),complex(1)],[complex(1),complex(0),complex(0),complex(0)],[complex(0),complex(1),complex(0),complex(0)]];

const oracles = [constant0, constant1, balancedX, balancedNotX];
const oracle = oracles[Math.floor(Math.random() * oracles.length)];

const zero = kZero();
const one = kOne();
const phi0 = mKron(kZero(), kOne());

const HkH = mKron(mHadamard(), mHadamard());
const phi1 = mMultiply(HkH, phi0);
const phi2 = mMultiply(oracle, phi1);

const HkI = mKron(mHadamard(), mIdentity());
const phi3 = mMultiply(HkI, phi2);

const result = measure(ignore(phi3, 1));
if (result === 0) {
  console.log('Function is constant');
} else {
  console.log('Function is balanced');
}

A quantum solution of Deutsch requires inspecting the oracle just once!

Quantum Algorithms

Deutsch - Jozsa

Generalised Deutsch algorithm for bigger inputs

f : \{0,1\}^n \to \{0,1\}
\begin{aligned} \text{Constant:} \quad & f(n) = 0 \quad \text{or} \quad f(n) = 1 \\ \text{Balanced:} \quad & f(n) = 0 \quad \text{for exactly half the inputs} \\ & f(n) = 1 \quad \text{for the other half} \end{aligned}

Quantum Algorithms

Deutsch - Jozsa: Classical

const oracle = ORACLES[Math.floor(Math.random() * oracles.length)];

function isConstant (f, n) {
  const first = f(0);
  for (let i = 1; i < 2 ** n; i++) {
    if (f(i) !== first) {
      return false;
    }
  }
  return true;
}

const constant = isConstant(oracle, 3);
const balanced = !constant;

A classical solution requires inspecting the oracle 2  times!

Quantum Algorithms

Deutsch - Jozsa: Quantum

|\psi_0\rangle = |0\rangle \otimes |0\rangle \otimes |1\rangle = \begin{bmatrix}0\\1\\0\\0\\0\\0\\0\\0\end{bmatrix}
H^{\otimes 3} = \begin{bmatrix} 1 & 1 & 1 & 1 & 1 & 1 & 1 & 1 \\ 1 & -1 & 1 & -1 & 1 & -1 & 1 & -1 \\ 1 & 1 & -1 & -1 & 1 & 1 & -1 & -1 \\ 1 & -1 & -1 & 1 & 1 & -1 & -1 & 1 \\ 1 & 1 & 1 & 1 & -1 & -1 & -1 & -1 \\ 1 & -1 & 1 & -1 & -1 & 1 & -1 & 1 \\ 1 & 1 & -1 & -1 & -1 & -1 & 1 & 1 \\ 1 & -1 & -1 & 1 & -1 & 1 & 1 & -1 \end{bmatrix}

Initial state with three qubits

Hadamard gate for three qubits

Quantum Algorithms

Deutsch - Jozsa: Quantum

|\psi_1\rangle = H^{\otimes 3} |\psi_0\rangle = \begin{bmatrix} 1\\-1\\1\\-1\\1\\-1\\1\\-1 \end{bmatrix}

Apply Hadamard gate to get superposition

Quantum Algorithms

Deutsch - Jozsa: Quantum

Black Box "Oracle" for the unknown function

U_f = \begin{bmatrix} ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? \\ ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? \\ ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? \\ ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? \\ ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? \\ ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? \\ ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? \\ ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? \\ ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? \\ ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? \\ ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? \\ ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? \\ ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? \\ ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? \\ ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? \\ ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? \\ ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? & ? \\ \end{bmatrix}

Size will increase exponentially with number of qubits

|\psi_2\rangle = U_f |\psi_1\rangle

Apply the Oracle to the superpositioned state

Quantum Algorithms

Deutsch - Jozsa: Quantum

H^{\otimes 2} \otimes I = \begin{bmatrix} 1 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & 1 & 0 & 0 & 0 & 0 \\ 1 & 0 & -1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1 & 0 & -1 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 1 & 0 & 1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & 1 \\ 0 & 0 & 0 & 0 & 1 & 0 & -1 & 0 \\ 0 & 0 & 0 & 0 & 0 & 1 & 0 & -1 \end{bmatrix}
|\psi_3\rangle = (H^{\otimes 2} \otimes I) |\psi_2\rangle

First N-1 Qubits Hadamard gate 

Apply the gate to the state

Quantum Algorithms

Deutsch - Jozsa: Quantum

|00\rangle \Rightarrow \text{function is constant} \\ \text{any other state} \Rightarrow \text{function is balanced}
\text{Measure the first N-1 Qubits of } |\psi_3\rangle

The second qubit contains redundant information

Quantum Algorithms

Deutsch - Jozsa: Quantum

const oracle = [
  // ...
];

let x = kZero();
for (let i = 1; i < n; i++) {
  x = mKron(x, kZero());
}
let y = kOne();
const phi0 = mKron(x, y);

const Hn = mKron(...Array(n).fill(mHadamard()));
const HnH = mKron(Hn, mHadamard());
const phi1 = mMultiply(HnH, phi0);

const phi2 = mMultiply(oracle, phi1);

const HnI = mKron(Hn, mIdentity());
const phi3 = mMultiply(HnI, phi2);

const outcome = measure(ignore(phi3, 1));
if (outcome === 0) {
  console.log("Function is constant");
} else {
  console.log("Function is balanced");
}

A quantum solution still requires inspecting the oracle just once!

OH HI CRAIG IT'S DARREN/MODOK. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

Craig! It's Cassie, Scott's daughter!

Cassie Lang

We've figured out how to open dad's cell! You need to run some Quantum Algorithms!

Cassie Lang

It's Deutsch-Josza, Grover, Shor, in that order! But we don't know the inputs!?

Cassie Lang

DEUTSCH-JOSZA

OH HI CRAIG IT'S DARREN/MODOK. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

Craig! It's Cassie, Scott's daughter!

Cassie Lang

We've figured out how to open dad's cell! You need to run some Quantum Algorithms!

Cassie Lang

It's Deutsch-Josza, Grover, Shor, in that order! But we don't know the inputs!?

Cassie Lang

DEUTSCH-JOSZA

THAT WORKED. THE FIRST LOCK OPENED! 🐜

Scott Lang

OH HI CRAIG IT'S DARREN/MODOK. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

Craig! It's Cassie, Scott's daughter!

Cassie Lang

We've figured out how to open dad's cell! You need to run some Quantum Algorithms!

Cassie Lang

It's Deutsch-Josza, Grover, Shor, in that order! But we don't know the inputs!?

Cassie Lang

DEUTSCH-JOSZA

THAT WORKED. THE FIRST LOCK OPENED! 🐜

Scott Lang

Quantum Algorithms

Grover's

f(x^*) = 1, \quad f(x) = 0 \ \text{for all other} \ x

Finds a marked item in an unsorted database of N items.

Quantum Search Algorithm

Quantum Algorithms

Grover's: Classical

const N = 8;
const marked = 5;

const result = [...Array(N).keys()].find(x => x === marked);
console.log(result);

A classical solution of Grovers requires inspecting each item once.

Quantum Algorithms

Grover's: Quantum

|\psi_0\rangle = |0\rangle \otimes |0\rangle = \begin{bmatrix}1\\0\\0\\0\end{bmatrix}

Initial state with two qubits

Quantum Algorithms

Grover's: Quantum

|\psi_1\rangle = (H \otimes H) |\psi_0\rangle = \begin{bmatrix}1\\1\\1\\1\end{bmatrix}
H \otimes H = \begin{bmatrix} 1 & 1 & 1 & 1 \\ 1 & -1 & 1 & -1 \\ 1 & 1 & -1 & -1 \\ 1 & -1 & -1 & 1 \end{bmatrix}

Hadamard gate for two qubits

Apply Hadamard gate to get superposition

Quantum Algorithms

Grover's: Quantum

|\psi_2\rangle = U_f |\psi_1\rangle = \begin{bmatrix}1\\1\\-1\\1\end{bmatrix}

Black Box "Oracle" for the search function

U_2 = \begin{bmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & -1 & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix}

Apply the Oracle to the superpositioned state

Quantum Algorithms

Grover's: Quantum

\text{Diffusion operator} = 2|\psi_s\rangle\langle \psi_s| - I
D = \begin{bmatrix} -1 & 1 & 1 & 1 \\ 1 & -1 & 1 & 1 \\ 1 & 1 & -1 & 1 \\ 1 & 1 & 1 & -1 \end{bmatrix}

Fancy operator made up of multiple gates

Quantum Algorithms

Grover's: Quantum

|\psi_3\rangle = D |\psi_2\rangle = \begin{bmatrix}1\\1\\3\\1\end{bmatrix}
|10\rangle \text{has the highest probability}

Apply diffusion 

\sqrt{N}

times

Quantum Algorithms

Grover's: Quantum

const N = Math.pow(2, n);

let state = kZero();
for (let i = 1; i < n; i++) {
  state = mKron(state, kZero());
}

let Hn = mHadamard();
for (let i = 1; i < n; i++) {
  Hn = mKron(Hn, mHadamard());
}
state = mMultiply(Hn, state);

const ones = Array.from({ length: N }, () => Array.from({ length: N }, () => complex(1 / N)));
const I = mIdentity(n);
const D = Array.from({ length: N }, (_, r) => Array.from({ length: N }, (_, c) => {
    const [onesReal] = ones[r][c];
    const [identityReal] = I[r][c];
    return complex(2 * onesReal - identityReal, 0);
}));

const iterations = Math.max(Math.floor((Math.PI / 4) * Math.sqrt(N)), 1);
for (let i = 0; i < iterations; i++) {
    state = mMultiply(oracle, state);
    state = mMultiply(D, state);
}

console.log(measure(state));

The quantum algorithm requires only             iterations!

\sqrt{N}

OH HI CRAIG IT'S DARREN/MODOK. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

Craig! It's Cassie, Scott's daughter!

Cassie Lang

We've figured out how to open dad's cell! You need to run some Quantum Algorithms!

Cassie Lang

It's Deutsch-Josza, Grover, Shor, in that order! But we don't know the inputs!?

Cassie Lang

GROVER'S

OH HI CRAIG IT'S DARREN/MODOK. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

Craig! It's Cassie, Scott's daughter!

Cassie Lang

We've figured out how to open dad's cell! You need to run some Quantum Algorithms!

Cassie Lang

It's Deutsch-Josza, Grover, Shor, in that order! But we don't know the inputs!?

Cassie Lang

GROVER'S

NICE! The second lock is open!

Cassie Lang

OH HI CRAIG IT'S DARREN/MODOK. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

Craig! It's Cassie, Scott's daughter!

Cassie Lang

We've figured out how to open dad's cell! You need to run some Quantum Algorithms!

Cassie Lang

It's Deutsch-Josza, Grover, Shor, in that order! But we don't know the inputs!?

Cassie Lang

GROVER'S

NICE! The second lock is open!

Cassie Lang

Quantum Algorithms

Prime Factorisation: Classical

let n = 391;

const factors = [];
for (let i = 2; i < n; i++) {
  while (n % i === 0) {
    factors.push(i);
    n = n / i;
  }
}

if (n > 1) {
  factors.push(n);
}

console.log(factors);
\quad O(\sqrt{N})

Not web scale

Quantum Algorithms

Shor's Algorithm

It efficiently finds non-trivial factors of a composite number NNN

f(x) = a^x \bmod N \;\xrightarrow{\text{period } r} \;\xrightarrow{\text{gcd}} \; [p, q]
f(11,413) = [101, 113]

Quantum Algorithms

Shor's Algorithm

let a;
do {
  a = Math.floor(Math.random() * (N - 2)) + 2;
} while (gcd(a, N) !== 1);

console.log(`Chosen a = ${a}`);

const n = Math.ceil(Math.log2(N));
const m = Math.ceil(Math.log2(N));

let input = kZero();
for (let i = 1; i < n; i++) input = mKron(input, kZero());
let output = kZero();
for (let i = 1; i < m; i++) output = mKron(output, kZero());
let phi0 = mKron(input, output);
const Hn = Array.from({ length: n }, () => mHadamard());
const HnKron = mKron(...Hn, mIdentity(m));
let phi1 = mMultiply(HnKron, phi0);

const U_f = modExpMatrix(a, N, n);
const UfFull = mKron(U_f, mIdentity(m));
let phi2 = mMultiply(UfFull, phi1);

const QFTn = qft(n);
const QFTFull = mKron(QFTn, mIdentity(m));
let phi3 = mMultiply(QFTFull, phi2);

const measured = measure(ignore(phi3, m));
console.log(`Estimate of period fraction: ${measured}`);

let r = 1;
while (modExp(a, r, N) !== 1) r++;
console.log("Estimated period r =", r);

if (r % 2 === 0) {
  const factor1 = gcd(modExp(a, r / 2, N) - 1, N);
  const factor2 = gcd(modExp(a, r / 2, N) + 1, N);
  console.log(`Potential factors: ${factor1}, ${factor2}`);
} else {
  console.log("Odd period found; retry with another a");
}

OH HI CRAIG IT'S DARREN/MODOK. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

Craig! It's Cassie, Scott's daughter!

Cassie Lang

We've figured out how to open dad's cell! You need to run some Quantum Algorithms!

Cassie Lang

It's Deutsch-Josza, Grover, Shor, in that order! But we don't know the inputs!?

Cassie Lang

SHOR'S

OH HI CRAIG IT'S DARREN/MODOK. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

Craig! It's Cassie, Scott's daughter!

Cassie Lang

We've figured out how to open dad's cell! You need to run some Quantum Algorithms!

Cassie Lang

It's Deutsch-Josza, Grover, Shor, in that order! But we don't know the inputs!?

Cassie Lang

SHOR'S

WHAT! HOW ARE YOU DOING THIS!

MODOK

OH HI CRAIG IT'S DARREN/MODOK. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

Craig! It's Cassie, Scott's daughter!

Cassie Lang

We've figured out how to open dad's cell! You need to run some Quantum Algorithms!

Cassie Lang

It's Deutsch-Josza, Grover, Shor, in that order! But we don't know the inputs!?

Cassie Lang

SHOR'S

WHAT! HOW ARE YOU DOING THIS!

MODOK

OH HI CRAIG IT'S DARREN/MODOK. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

Craig! It's Cassie, Scott's daughter!

Cassie Lang

We've figured out how to open dad's cell! You need to run some Quantum Algorithms!

Cassie Lang

It's Deutsch-Josza, Grover, Shor, in that order! But we don't know the inputs!?

Cassie Lang

I'M OUT! THANKS CRAIG! 🐜

Scott Lang

OH HI CRAIG IT'S DARREN/MODOK. I HAVE SCOTT.

MODOK

HE'S NEVER GETTING OUT OF HERE.

MODOK

Craig! It's Cassie, Scott's daughter!

Cassie Lang

We've figured out how to open dad's cell! You need to run some Quantum Algorithms!

Cassie Lang

It's Deutsch-Josza, Grover, Shor, in that order! But we don't know the inputs!?

Cassie Lang

I'M OUT! THANKS CRAIG! 🐜

Scott Lang

THE END