Skip to content

Commit

Permalink
Speed up random circuit creation (Qiskit#3249)
Browse files Browse the repository at this point in the history
The qiskit.circuit.random.random_circuit() function was previously using
signature inspection to figure out how many angles a gate needed. While
this is fine for smaller circuit creation, when the circuits get really
large the time it takes the inspect module to return the signature
starts to add up. The use of the signature check isn't required because
the number of parameters for gates are fixed. This commit speeds random
circuit creation by creating a list of each gate with parameters and
just checking if the randomly selected operation is present in one of
those.
  • Loading branch information
mtreinish authored and ajavadia committed Oct 12, 2019
1 parent e5870a4 commit d84b0bb
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions qiskit/circuit/random/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

"""Utility functions for generating random circuits."""

from inspect import signature
import numpy as np

from qiskit.circuit import QuantumRegister, ClassicalRegister, QuantumCircuit
Expand Down Expand Up @@ -52,6 +51,9 @@ def random_circuit(n_qubits, depth, max_operands=3, measure=False,

one_q_ops = [IdGate, U1Gate, U2Gate, U3Gate, XGate, YGate, ZGate,
HGate, SGate, SdgGate, TGate, TdgGate, RXGate, RYGate, RZGate]
one_param = [U1Gate, RXGate, RYGate, RZGate, RZZGate, Cu1Gate, CrzGate]
two_param = [U2Gate]
three_param = [U3Gate, Cu3Gate]
two_q_ops = [CnotGate, CyGate, CzGate, CHGate, CrzGate,
Cu1Gate, Cu3Gate, SwapGate, RZZGate]
three_q_ops = [ToffoliGate, FredkinGate]
Expand Down Expand Up @@ -86,10 +88,14 @@ def random_circuit(n_qubits, depth, max_operands=3, measure=False,
operation = rng.choice(two_q_ops)
elif num_operands == 3:
operation = rng.choice(three_q_ops)
op_args = list(signature(operation).parameters.keys())
num_angles = len(op_args)
if 'label' in op_args: # TODO: label is not part of gate params and must be removed
num_angles -= 1
if operation in one_param:
num_angles = 1
elif operation in two_param:
num_angles = 2
elif operation in three_param:
num_angles = 3
else:
num_angles = 0
angles = [rng.uniform(0, 2*np.pi) for x in range(num_angles)]
register_operands = [qr[i] for i in operands]
op = operation(*angles)
Expand Down

0 comments on commit d84b0bb

Please sign in to comment.