Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve circuit compilation performance #375

Merged
merged 1 commit into from
Nov 30, 2024

Conversation

ohuettenhofer
Copy link
Contributor

@ohuettenhofer ohuettenhofer commented Nov 30, 2024

This PR speeds up working with and compiling large circuits.

Here is a simple benchmark to test this:

Code
import tequila as tq
import random
from time import time

if __name__ == "__main__":
    random.seed(0)
    start = time()

    circuit = tq.QCircuit()
    for i in range(100):
        subcircuit = tq.QCircuit()
        for j in range(100):
            qubits = random.sample(range(10), 3)
            subcircuit += tq.gates.Toffoli(qubits[0], qubits[1], qubits[2])
            subcircuit += tq.gates.Ry(0.5, random.choice(range(10)))
        circuit += subcircuit

    daggered = circuit.dagger()
    circuit += daggered

    tq.simulate(circuit, backend="qulacs", optimize_circuit=False)

    print(f"Time: {time() - start:.3f} s")

Having optimize_circuit=False is necessary, without it the Qulacs optimization will take very long.

Before this pull request, the script took about 17 seconds on my machine.

The first change is to revert #360, because I didn't realize how big the performance impact of the deepcopy call here is.
I don't like removing this, but it reduces the runtime to around 10 seconds, and it seems like it was fine without the copy for years before I made that pull request.
I extended the warning message to explain the specific case that made me add the copy.

The second change is optimizing the replace_gates function, because it is currently implemented in an inefficient way that performs a lot of unnecessary copying. This change reduces the runtime of the script to about 3 seconds.

I also fixed a problem in the QubitWaveFunction length method where it also counted zeros, and added a function for BitString that returns the stored integer in a specified bit ordering.

@kottmanj
Copy link
Collaborator

Thanks!
Looks like it would make sense to think about a criterium to chose the default value of optimize_circuit

@kottmanj kottmanj merged commit a12e33f into tequilahub:devel Nov 30, 2024
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants