Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
d3a5160
Remove stale TODO
tanujkhattar Jun 29, 2022
198c2f7
Merge branch 'master' of https://github.com/quantumlib/cirq
tanujkhattar Jun 29, 2022
cc4c875
Merge branch 'master' of https://github.com/quantumlib/cirq
tanujkhattar Jun 30, 2022
ef3f3df
Merge branch 'master' of https://github.com/quantumlib/cirq
tanujkhattar Jul 15, 2022
badb27e
Merge branch 'master' of https://github.com/quantumlib/cirq
tanujkhattar Jul 16, 2022
232a6d5
Merge branch 'master' of https://github.com/quantumlib/cirq
tanujkhattar Jul 18, 2022
7cebae5
Merge branch 'master' of https://github.com/quantumlib/cirq
tanujkhattar Jul 23, 2022
8700998
Merge branch 'master' of https://github.com/quantumlib/Cirq
tanujkhattar Jul 23, 2022
3159fdc
Merge branch 'master' of https://github.com/quantumlib/Cirq
tanujkhattar Aug 22, 2022
4fb7546
Merge branch 'master' of https://github.com/quantumlib/Cirq
tanujkhattar Aug 25, 2022
2b2e464
Merge branch 'master' of https://github.com/quantumlib/cirq
tanujkhattar Aug 26, 2022
49152b1
Merge branch 'master' of https://github.com/quantumlib/cirq
tanujkhattar Aug 29, 2022
d99ce0f
Merge branch 'master' of https://github.com/quantumlib/cirq
tanujkhattar Aug 29, 2022
27142dc
Fix asv setup and add benchmarks for circuit construction
tanujkhattar Aug 29, 2022
16ff38a
Add benchmarks for both operation-by-operation and moment-by-moment
tanujkhattar Aug 31, 2022
e3380ae
Merge branch 'master' of https://github.com/quantumlib/cirq into asv_…
tanujkhattar Sep 2, 2022
b12a13d
Address comments
tanujkhattar Sep 2, 2022
72f7c6f
Merge branch 'master' into asv_update
tanujkhattar Sep 2, 2022
14794c3
Merge branch 'master' into asv_update
CirqBot Sep 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions asv.conf.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
// See https://asv.readthedocs.io/en/stable/asv.conf.json.html for documentation.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why drop this link? Is ASV sufficiently well-known that readers can be expected to know what this file is without it?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Json files don't usually support comments, so I removed the comment to avoid confusion for anyone reading the json file and wondering why would this work.

"version": 1,
"project": "Cirq",
"project_url": "https://quantumai.google/cirq",
"repo": ".",
"repo_subdir": "cirq-core/",
"branches": ["master"],
"dvcs": "git",
"environment_type": "virtualenv",
"show_commit_url": "https://github.com/quantumlib/Cirq/commit/",
"pythons": ["3.7", "3.8"],
"pythons": ["3.8"],
"benchmark_dir": "benchmarks",
"env_dir": ".asv/env",
"results_dir": ".asv/results",
Expand Down
63 changes: 0 additions & 63 deletions benchmarks/bench_examples.py

This file was deleted.

5 changes: 1 addition & 4 deletions benchmarks/bench_linalg_decompositions.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,5 @@ def time_kak_decomposition(target):
cirq.kak_decomposition(target)


time_kak_decomposition.params = [ # type: ignore
[np.eye(4), SWAP, SWAP * 1j, CZ, CNOT, SWAP.dot(CZ)]
+ [cirq.testing.random_unitary(4) for _ in range(10)]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume these random unitaries were producing misleading variance in the benchmark?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main reason to remove is that their string representation (used in benchmarks by default to represent lines) currently shows up as the entire matrix, which clutters the benchmark a lot.

See https://cirq-infra.uc.r.appspot.com/#bench_linalg_decompositions.time_kak_decomposition for example

]
time_kak_decomposition.params = [cirq.IdentityGate(2), cirq.SWAP, cirq.ISWAP, cirq.CZ, cirq.CNOT]
time_kak_decomposition.param_names = ["gate"] # type: ignore
136 changes: 136 additions & 0 deletions benchmarks/circuit_construction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# Copyright 2022 The Cirq Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import itertools
from typing import Tuple, Set, Sequence

import cirq


def rotated_surface_code_memory_z_cycle(
data_qubits: Set[cirq.Qid],
z_measure_qubits: Set[cirq.Qid],
x_measure_qubits: Set[cirq.Qid],
z_order: Sequence[Tuple[int, int]],
x_order: Sequence[Tuple[int, int]],
) -> cirq.Circuit:
"""Constructs a circuit for a single round of rotated memory Z surface code.

Args:
data_qubits: data qubits for the surface code patch.
z_measure_qubits: measure qubits to measure Z stabilizers for surface code patch.
x_measure_qubits: measure qubits to measure X stabilizers for surface code patch.
z_order: Specifies the order in which the 2/4 data qubit neighbours of a Z measure qubit
should be processed.
x_order: Specifies the order in which the 2/4 data qubit neighbours of a X measure qubit
should be processed.

Returns:
A `cirq.Circuit` for a single round of rotated memory Z surface code cycle.
"""

circuit = cirq.Circuit()
circuit += cirq.Moment([cirq.H(q) for q in x_measure_qubits])
for k in range(4):
op_list = []
for measure_qubits, add, is_x in [
[x_measure_qubits, x_order[k], True],
[z_measure_qubits, z_order[k], False],
]:
for q_meas in measure_qubits:
q_data = q_meas + add
if q_data in data_qubits:
op_list.append(cirq.CNOT(q_meas, q_data) if is_x else cirq.CNOT(q_data, q_meas))
circuit += cirq.Moment(op_list)
circuit += cirq.Moment([cirq.H(q) for q in x_measure_qubits])
circuit += cirq.Moment(cirq.measure_each(*x_measure_qubits, *z_measure_qubits))
return circuit


def surface_code_circuit(
distance: int, num_rounds: int, moment_by_moment: bool = True
) -> cirq.Circuit:
"""Constructs a rotated memory Z surface code circuit with `distance` and `num_rounds`.

The circuit has `dxd` data qubits and `d ** 2 - 1` measure qubits, where `d` is the distance
of surface code. For more details on rotated surface codes and qubit indexing, see figure 13
https://arxiv.org/abs/1111.4022.

Args:
distance: Distance of the surface code.
num_rounds: Number of error correction rounds for memory Z experiment.
moment_by_moment: If True, the circuit is constructed moment-by-moment instead of
operation-by-operation. This is useful to benchmark different circuit construction
patterns for the same circuit.

Returns:
A `cirq.Circuit` for surface code memory Z experiment for `distance` and `num_rounds`.
"""

def ndrange(*ranges: Tuple[int, ...]):
return itertools.product(*[range(*r) for r in ranges])

data_qubits = {cirq.q(2 * x + 1, 2 * y + 1) for x, y in ndrange((distance,), (distance,))}
z_measure_qubits = {
cirq.q(2 * x, 2 * y) for x, y in ndrange((1, distance), (distance + 1,)) if x % 2 != y % 2
}
x_measure_qubits = {
cirq.q(2 * x, 2 * y) for x, y in ndrange((distance + 1,), (1, distance)) if x % 2 == y % 2
}
x_order = [(1, 1), (1, -1), (-1, 1), (-1, -1)]
z_order = [(1, 1), (-1, 1), (1, -1), (-1, -1)]
surface_code_cycle = rotated_surface_code_memory_z_cycle(
data_qubits, x_measure_qubits, z_measure_qubits, x_order, z_order
)
if moment_by_moment:
return cirq.Circuit(
surface_code_cycle * num_rounds, cirq.Moment(cirq.measure_each(*data_qubits))
)
else:
return cirq.Circuit(
[*surface_code_cycle.all_operations()] * num_rounds, cirq.measure_each(*data_qubits)
)


class SurfaceCodeRotatedMemoryZ:
pretty_name = "Surface Code Rotated Memory-Z Benchmarks."
params = [*range(3, 26, 2)]
param_names = ["distance"]

def time_circuit_construction_moment_by_moment(self, distance: int) -> None:
"""Benchmark circuit construction for Rotated Bottom-Z Surface code."""
_ = surface_code_circuit(distance, distance * distance)

def time_circuit_construction_operations_by_operation(self, distance: int) -> None:
"""Benchmark circuit construction for Rotated Bottom-Z Surface code."""
_ = surface_code_circuit(distance, distance * distance, False)

def track_circuit_operation_count(self, distance: int) -> int:
"""Benchmark operation count for Rotated Bottom-Z Surface code."""
circuit = surface_code_circuit(distance, distance * distance)
return sum(1 for _ in circuit.all_operations())

def track_circuit_depth(self, distance: int) -> int:
"""Benchmark operation count for Rotated Bottom-Z Surface code."""
circuit = surface_code_circuit(distance, distance * distance)
return len(circuit)


class XOnAllQubitsCircuit:
pretty_name = "N * D times X gate on all qubits."
params = [[1, 10, 100, 1000], [1, 10, 100, 1000]]
param_names = ["Number of Qubits(N)", "Depth(D)"]

def time_circuit_construction(self, N: int, D: int):
q = cirq.LineQubit.range(N)
return cirq.Circuit(cirq.Moment(cirq.X.on_each(*q)) for _ in range(D))