Cirq
Cirq copied to clipboard
Speed up Circuit construction
We should see if we can get the circuit construction time down, perhaps by invoking a builder pattern.
Here is an example of the speed of circuit construction (variance will be quite high, just doing a single time), but this is good for order of magnitudes
import cirq
import time
import matplotlib.pyplot as plt
def build_circuit(n, d):
qubits = cirq.GridQubit.rect(n, 1)
c = cirq.Circuit()
for _ in range(d):
for q in qubits:
c.append(cirq.X(q))
return c
xs = []
ys = []
for n in [1, 10, 100, 1000,]:
for d in [1, 10, 100, 1000,]:
start = time.time()
build_circuit(n, d)
delta = time.time() - start
xs.append(n * d)
ys.append(delta)
plt.xlabel("Number of qubits times depth")
plt.ylabel("Time (seconds)")
plt.loglog(xs, ys, 'o')
which produces the following
If you create a list of the qubits and then create a circuit from the list, that should give much faster construction times than appending to the circuit one-by-one.
From Cirq sync:
- https://github.com/quantumlib/Cirq/pull/5845 contains benchmarks to help define goals for performance improvements
- Please help add any improvements you can think of in the meantime!
- Document the behavior that adding to a list and then constructing circuit is much faster than appending operations one by one.