I am trying to make a circuit for the randomly selected gates from a gate_list but I don't know how to put these selected gates in front of qr. statement. I don't know how to do this exactly is there any batter way to do this?

from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit import Aer, execute
import random

n = 8
gate_list = ['u1', 'u2', 'u3', 'id', 'x', 'y', 'z', 'h', 's'] 

selected_gates= []

for i in range(0,8):
  x = random.choice(gates)
  a = '({})'.format(i)
  k = x+a

qr = QuantumCircuit(n)


1 Answer 1


This is very similar to an function in terra random_circuit: https://github.com/Qiskit/qiskit-terra/blob/master/qiskit/circuit/random/utils.py#L30-L113 It randomly picks gates from the list of all the standard gates in terra. For example, you can run something like:

from qiskit.circuit.random import random_circuit

qr = random_circuit(10, 10, max_operands=3, measure=True)

Which will return a 10 qubit circuit of depth 10 with measurements at the end.

What this function is missing is an option to specify the list of gates to select from, but I think if you opened an issue about this no one would be opposed to adding that functionality in the future. But, regardless the implementation of that function shows how you would implement a version with your own list of gates, just make a local copy of the function and modify the lists https://github.com/Qiskit/qiskit-terra/blob/master/qiskit/circuit/random/utils.py#L52-L59.

  • 1
    $\begingroup$ Is there any function in qiskit to see stavevector in the end of the circuit?@Matthew Treinish $\endgroup$
    – zircon
    Commented Feb 22, 2020 at 14:11
  • 2
    $\begingroup$ You'll have to use the statevector simulator, something like: ``` from qiskit import Aer from qiskit import execute from qiskit.circuit.random import random_circuit qr = random_circuit(10, 10, max_operands=3) backend = Aer.get_backend('statevector_simulator') sv = execute(qr, backend).result().get_statevector() print(sv) ``` $\endgroup$ Commented Feb 22, 2020 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.