Generate a pseudo random linear circuit.
(
num_qubits,
num_gates,
seed=None,
barrier=False,
delay=False,
permutation=False,
linear=False,
clifford=False,
recursion_depth=0,
)
| 27 | |
| 28 | |
| 29 | def random_linear_circuit( |
| 30 | num_qubits, |
| 31 | num_gates, |
| 32 | seed=None, |
| 33 | barrier=False, |
| 34 | delay=False, |
| 35 | permutation=False, |
| 36 | linear=False, |
| 37 | clifford=False, |
| 38 | recursion_depth=0, |
| 39 | ): |
| 40 | """Generate a pseudo random linear circuit.""" |
| 41 | |
| 42 | if num_qubits == 0: |
| 43 | raise CircuitError("Cannot construct a random linear circuit with 0 qubits.") |
| 44 | |
| 45 | circ = QuantumCircuit(num_qubits) |
| 46 | |
| 47 | instructions = ["cx", "swap"] if num_qubits >= 2 else [] |
| 48 | if barrier: |
| 49 | instructions.append("barrier") |
| 50 | if delay: |
| 51 | instructions.append("delay") |
| 52 | if permutation: |
| 53 | instructions.append("permutation") |
| 54 | if linear: |
| 55 | instructions.append("linear") |
| 56 | if clifford: |
| 57 | instructions.append("clifford") |
| 58 | if recursion_depth > 0: |
| 59 | instructions.append("nested") |
| 60 | |
| 61 | if not instructions: |
| 62 | # Return the empty circuit if there are no instructions to choose from. |
| 63 | return circ |
| 64 | |
| 65 | if isinstance(seed, np.random.Generator): |
| 66 | rng = seed |
| 67 | else: |
| 68 | rng = np.random.default_rng(seed) |
| 69 | |
| 70 | name_samples = rng.choice(instructions, num_gates) |
| 71 | |
| 72 | for name in name_samples: |
| 73 | if name == "cx": |
| 74 | qargs = rng.choice(range(num_qubits), 2, replace=False).tolist() |
| 75 | circ.cx(*qargs) |
| 76 | elif name == "swap": |
| 77 | qargs = rng.choice(range(num_qubits), 2, replace=False).tolist() |
| 78 | circ.swap(*qargs) |
| 79 | elif name == "barrier": |
| 80 | nqargs = rng.choice(range(1, num_qubits + 1)) |
| 81 | qargs = rng.choice(range(num_qubits), nqargs, replace=False).tolist() |
| 82 | circ.barrier(qargs) |
| 83 | elif name == "delay": |
| 84 | qarg = rng.choice(range(num_qubits)) |
| 85 | circ.delay(100, qarg) |
| 86 | elif name == "linear": |
no test coverage detected