Skip to content

Instantly share code, notes, and snippets.

View Strilanc's full-sized avatar

Craig Gidney Strilanc

View GitHub Profile
@Strilanc
Strilanc / distill_t_using_cs.py
Created October 11, 2022 18:53
Distill a T state using on average 22 CS gates
import random
import cirq
import numpy as np
def fresh_attempt() -> int:
a, b = cirq.LineQubit.range(2)
h_cx_cs_h = cirq.unitary(cirq.Circuit(cirq.H(b), cirq.X(a).controlled_by(b), cirq.S(a).controlled_by(b), cirq.H(b)))
check_output_tdag_h = cirq.unitary(cirq.Circuit(cirq.T(a) ** -1, cirq.H(a)))
@Strilanc
Strilanc / epr_distill_5q.stim
Created September 2, 2022 14:11
This is a stim circuit that implements entanglement distillation using the 5 qubit perfect code. Basically you share five noisy EPR pairs between Alice and Bob, then Alice and Bob both measure the 5Q code stabilizers, and they discard whenever their measured stabilizers disagree. For a depolarization strength of 1e-3 this results in a discard ra…
# Quirk testing URL:
# https://algassert.com/quirk#circuit=%7B%22cols%22%3A%5B%5B%22H%22%2C%22H%22%2C%22H%22%2C%22H%22%2C%22H%22%5D%2C%5B%22%E2%80%A2%22%2C1%2C1%2C1%2C1%2C%22X%22%5D%2C%5B1%2C%22%E2%80%A2%22%2C1%2C1%2C1%2C1%2C%22X%22%5D%2C%5B1%2C1%2C%22%E2%80%A2%22%2C1%2C1%2C1%2C1%2C%22X%22%5D%2C%5B1%2C1%2C1%2C%22%E2%80%A2%22%2C1%2C1%2C1%2C1%2C%22X%22%5D%2C%5B1%2C1%2C1%2C1%2C%22%E2%80%A2%22%2C1%2C1%2C1%2C1%2C%22X%22%5D%2C%5B%22xpar%22%2C%22zpar%22%2C%22xpar%22%2C%22xpar%22%2C1%2C%220%22%5D%2C%5B%22Measure%22%5D%2C%5B%22zpar%22%2C1%2C%22zpar%22%2C%22xpar%22%2C%22zpar%22%2C%220%22%5D%2C%5B1%2C1%2C1%2C1%2C%22H%22%5D%2C%5B1%2C1%2C1%2C1%2C%22Measure%22%5D%2C%5B%22zpar%22%2C%22xpar%22%2C%22xpar%22%2C1%2C%22zpar%22%2C%220%22%5D%2C%5B1%2C%22Measure%22%5D%2C%5B1%2C%22zpar%22%2C%22zpar%22%2C%22zpar%22%2C%22zpar%22%2C%220%22%5D%2C%5B1%2C1%2C1%2C%22H%22%5D%2C%5B1%2C1%2C1%2C%22Measure%22%5D%2C%5B1%2C1%2C1%2C1%2C1%2C%22xpar%22%2C%22zpar%22%2C%22xpar%22%2C%22xpar%22%2C1%2C1%2C1%2C1%2C1%2C1%2C%22X%22%5D%2C%5B1%2C1%2C1%2C1%2C1
from typing import Iterable, Tuple
import time
import numpy as np
def to_mask(word: str) -> int:
"""Converts a word into a bitmask where each bit indicates presence of a letter."""
t = 0
for c in word:
@Strilanc
Strilanc / stim_tableau_vs_circuit.py
Created July 13, 2022 23:59
Code to convert a stim.Circuit into an equivalent stim.Tableau and a stim.Tableau into an equivalent stim.Circuit. The circuit decomposition isn't efficient but it's simple to write.
from typing import List
import stim
def circuit_to_tableau(circuit: stim.Circuit) -> stim.Tableau:
s = stim.TableauSimulator()
s.do_circuit(circuit)
return s.current_inverse_tableau() ** -1
@Strilanc
Strilanc / scratch.py
Created July 7, 2022 17:03
Generating a depth 52 14-controlled-X using measurement based uncomputation
from typing import Optional, Dict
import cirq
def compose(*gates: cirq.Gate) -> cirq.Gate:
matrix = cirq.unitary(gates[0])
for g in gates[1:]:
matrix = cirq.unitary(g) @ matrix
return cirq.MatrixGate(matrix)
@Strilanc
Strilanc / increment_to_linear_toffoli.py
Created June 9, 2015 05:54
Hacky python code to build n-bit reversible increment gates out of O(n) Toffoli gates and one ancilla bit.
# coding=utf-8
import math
import itertools
def evaluate_circuit(gates, initial_bits=None):
"""
:param gates: [( [controls], [targets] )]
:param initial_bits: set(on_bits)
@Strilanc
Strilanc / webgl_cube.html
Created June 9, 2020 01:37
Chopped down webgl cube example
<html>
<body>
<script>
let canvas = document.createElement('canvas');
document.body.appendChild(canvas);
let gl = canvas.getContext('webgl');
if (!gl) {
throw new Error("!gl")
}
@Strilanc
Strilanc / minimal_web_xr.html
Created March 1, 2020 23:03
The bare minimum needed to render to an immersive headset.
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>WebXR Testing</title>
</head>
<body>
<input id="button_enter" type="button" value="Loading..." disabled/>
<script>
const GL = WebGLRenderingContext;
@Strilanc
Strilanc / sim-factor.py
Created May 9, 2019 08:15
Simulates samples that Shor's algorithm would generate, in order to estimate how often factoring succeeds.
from typing import Optional, List, Dict, Callable, Any
from collections import defaultdict
import fractions
import math
import random
import sys
import matplotlib.pyplot as plt
@Strilanc
Strilanc / high_persistence_of_multiplication_search.py
Created March 22, 2019 05:54
Code to search for numbers with large persistence of multiplication. Finds the largest known ones in under a second.
def persistence(n):
t = 0
while n >= 10:
t += 1
n2 = 1
for d in str(n):
n2 *= int(d)
n = n2
return t