Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize clifford benchmark circuit creation for a total 94.5% speedup #6401

Merged
merged 14 commits into from
Jan 8, 2024
25 changes: 16 additions & 9 deletions cirq-core/cirq/experiments/qubit_characterizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,11 +560,19 @@ def _measurement(two_qubit_circuit: circuits.Circuit) -> np.ndarray:


def _create_parallel_rb_circuit(
qubits: Iterator['cirq.Qid'], num_cliffords: int, c1: list
qubits: Sequence['cirq.Qid'], num_cliffords: int, c1: list
) -> 'cirq.Circuit':
circuits_to_zip = [_random_single_q_clifford(qubit, num_cliffords, c1) for qubit in qubits]
circuit = circuits.Circuit.zip(*circuits_to_zip)
return circuits.Circuit.from_moments(*circuit, ops.measure_each(*qubits))
sequences_to_zip = [_random_single_q_clifford(qubit, num_cliffords, c1) for qubit in qubits]
# Ensure each sequence has the same number of moments.
num_moments = max(len(sequence) for sequence in sequences_to_zip)
for i in range(len(sequences_to_zip)):
if len(sequences_to_zip[i]) < num_moments:
sequences_to_zip[i].extend(
[ops.SingleQubitCliffordGate.I(qubits[i])]
* (num_moments - len(sequences_to_zip[i]))
)
NoureldinYosri marked this conversation as resolved.
Show resolved Hide resolved
moments = tuple(zip(*sequences_to_zip, strict=True))
NoureldinYosri marked this conversation as resolved.
Show resolved Hide resolved
return circuits.Circuit.from_moments(*moments, ops.measure_each(*qubits))


def _indices_after_basis_rot(i: int, j: int) -> Tuple[int, Sequence[int], Sequence[int]]:
Expand Down Expand Up @@ -609,13 +617,12 @@ def _two_qubit_clifford_matrices(

def _random_single_q_clifford(
qubit: 'cirq.Qid', num_cfds: int, cfds: Sequence[Sequence['cirq.Gate']]
) -> 'cirq.Circuit':
) -> List['cirq.Operation']:
clifford_group_size = 24
ops = [[gate(qubit) for gate in gates] for gates in cfds]
gate_ids = list(np.random.choice(clifford_group_size, num_cfds))
gate_sequence = [gate for gate_id in gate_ids for gate in cfds[gate_id]]
gate_sequence.append(_reduce_gate_seq(gate_sequence) ** -1)
circuit = circuits.Circuit(gate(qubit) for gate in gate_sequence)
return circuit
adjoint = _reduce_gate_seq([gate for gate_id in gate_ids for gate in cfds[gate_id]]) ** -1
return [op for gate_id in gate_ids for op in ops[gate_id]] + [adjoint(qubit)]


def _random_two_q_clifford(
Expand Down
Loading