def random_circuit(ngates, nqubits, gate_space=(X, Y, Z, S, T, H, CNOT, SWAP)):
 qubit_space = range(nqubits)
 result = []
 for i in range(ngates):
  g = random.choice(gate_space)
  if ((g == CNotGate) or (g == SwapGate)):
   qubits = random.sample(qubit_space, 2)
   g = g(*qubits)
  else:
   qubit = random.choice(qubit_space)
   g = g(qubit)
  result.append(g)
 return Mul(*result)

def random_circuit ( ngates , nqubits , gate_space = ( X , Y , Z , S , T , H , CNOT , SWAP ) ) : qubit_space = range ( nqubits ) result = [ ] for i in range ( ngates ) : g = random . choice ( gate_space ) if ( ( g == CNotGate ) or ( g == SwapGate ) ) : qubits = random . sample ( qubit_space , 2 ) g = g ( * qubits ) else : qubit = random . choice ( qubit_space ) g = g ( qubit ) result . append ( g ) return Mul ( * result )
return a random circuit of ngates and nqubits .