Skip to content

qc.qasm() for MCXGate or any gate with complex definition is slow #10641

@rht

Description

@rht

Environment

  • Qiskit Terra version: 0.25.0
  • Python version: 3.10
  • Operating system: Linux

What is happening?

The qc.qasm() step in the "how can we reproduce the issue?" section of this issue is very slow.
Upon profiling, 99.9% of the time is spent at https://github.com/Qiskit/qiskit-terra/blob/2062246191eb6c042c52aca6ef6de1d0233db314/qiskit/circuit/quantumcircuit.py#L5091 of _qasm2_define_custom_operation. Where 99.9% of the equality check elapsed happens in the instruction definition comparison.

I was able to speed up the code from ~17s to 0.0096s by replacing that line with if previous_definition_source is not None:, but this probably requires examination to make sure it is fine for all cases.

How can we reproduce the issue?

With the following code

import time
from qiskit import QuantumCircuit


nqubits = 5
qc = QuantumCircuit(nqubits)
for i in range(100):
    qc.mcx(list(range(nqubits - 1)), nqubits - 1)
tic = time.time()
qc.qasm()
print("Elapsed", time.time() - tic)

I found the elapsed to be ~17s.

What should happen?

It's only 100 gates, 5 qubits, and so should have been much faster.

Any suggestions?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmod: qasm2Relating to OpenQASM 2 import or exportperformance

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions