Cirq icon indicating copy to clipboard operation
Cirq copied to clipboard

Speed up Circuit construction

Open dabacon opened this issue 1 year ago • 2 comments

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 image

dabacon avatar Aug 22 '22 19:08 dabacon

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.

daxfohl avatar Aug 22 '22 21:08 daxfohl

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.

verult avatar Aug 31 '22 17:08 verult