Skip to content

Commit

Permalink
Merge branch 'main' into ci
Browse files Browse the repository at this point in the history
  • Loading branch information
Gistbatch authored Feb 7, 2024
2 parents ce1bf47 + 154f6bd commit 38e6d54
Show file tree
Hide file tree
Showing 35 changed files with 1,104 additions and 19,566 deletions.
35 changes: 13 additions & 22 deletions notebooks/logical_qubit_extended_memory_experiment.ipynb

Large diffs are not rendered by default.

66 changes: 39 additions & 27 deletions notebooks/logical_qubit_memory_experiment.ipynb

Large diffs are not rendered by default.

18,782 changes: 86 additions & 18,696 deletions notebooks/move_qubit_along_a_line.ipynb

Large diffs are not rendered by default.

377 changes: 0 additions & 377 deletions src/tqec/detectors/gate.py

This file was deleted.

113 changes: 88 additions & 25 deletions src/tqec/detectors/measurement_map.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import cirq
from tqec.exceptions import MeasurementAppliedOnMultipleQubitsException, TQECException

from tqec.exceptions import TQECException
from tqec.detectors.operation import RelativeMeasurementsRecord


def flatten(obj: cirq.Moment | cirq.AbstractCircuit) -> cirq.Circuit:
Expand All @@ -20,9 +22,10 @@ def __init__(self, circuit: cirq.AbstractCircuit) -> None:
"""Stores information about all the measurements found in the provided circuit
This class provides a method to recover the global record offset of a given
measurement from local informations about this measurement.
measurement from local information about this measurement.
:param circuit: the circuit instance to analyse.
Args:
circuit: the circuit instance to analyse.
"""
flattened_circuit = flatten(circuit)
(
Expand All @@ -40,21 +43,30 @@ def get_measurement_relative_offset(
with their local temporal description and to recover the global offset that should be
used to get the measurement result from the global measurement record.
:param current_moment_index: the moment index for which we want to compute the offset.
This method will only backtrack in time, and so will never return measurements that
are performed after the moment provided in this parameter. Also, the measurement
record offset is a local quantity that might change in time (due to subsequent
measurements shifting the offset), meaning that the returned offset should only be
considered valid for the moment provided here, and for no other moments.
:param qubit: qubit instance the measurement we are searching for has been performed on.
:param measurement_offset: the temporally-local, negative, measurement offset. A value of
-1 means "the last measurement performed on this qubit" ("last" should always be read
as "last from the current_moment_index moment view"), -2 means "the measurement just
before the last measurement performed on this qubit", etc.
:returns: the global measurement record offset, only valid for the provided
Args:
current_moment_index: the moment index for which we want to compute
the offset. This method will only backtrack in time, and so will
never return measurements that are performed after the moment
provided in this parameter. Also, the measurement record offset
is a local quantity that might change in time (due to subsequent
measurements shifting the offset), meaning that the returned
offset should only be considered valid for the moment provided
here, and for no other moments.
qubit: qubit instance the measurement we are searching for has been
performed on.
measurement_offset: the temporally-local, negative, measurement
offset. A value of -1 means "the last measurement performed on
this qubit" ("last" should always be read as "last from the
current_moment_index moment view"), -2 means "the measurement
just before the last measurement performed on this qubit", etc.
Returns:
the global measurement record offset, only valid for the provided
current_moment_index, or None if the searched offset does not exist.
:raises PositiveMeasurementOffsetError: if the provided measurement_offset value is positive.
Raises:
PositiveMeasurementOffsetError: if the provided measurement_offset
value is positive.
"""
if measurement_offset >= 0:
raise TQECException(
Expand Down Expand Up @@ -100,23 +112,30 @@ def _get_global_measurement_index(
"""Computes and returns the global measurement indices for the given circuit
This method API takes into account the fact that some information need to be passed
accross recursions and so is not really user-friendly.
across recursions and so is not really user-friendly.
The provided circuit should not contain any cirq.CircuitOperation instance.
:param circuit: circuit to compute the global measurements of.
:param _measurement_offset: offset applied to the indices of each measurements. Used for the
recursive calls.
:returns: a tuple (global_measurement_indices, global_measurement_index).
- global_measurement_indices is a list containing one entry for each Moment in the provided
Args:
circuit: circuit to compute the global measurements of.
_measurement_offset: offset applied to the indices of each
measurement. Used for the recursive calls.
Returns:
a tuple (global_measurement_indices, global_measurement_index). -
global_measurement_indices is a list containing one entry for each
Moment in the provided
circuit. The entry for a given Moment corresponds to a mapping from the qubit instance that
has been measured and the global measurement index (again, measurements in repeated
cirq.CircuitOperation instances are only counted once).
- global_measurement_index is an integer representing the index of the next measurement that
will be encountered. It is part of the return API to simplify the recursion, and should not
be useful for the external caller.
:raises MeasurementAppliedOnMultipleQubits: if the provided cirq.AbstractCircuit instance contains
measurements applied on several qubits at the same time.
Raises:
MeasurementAppliedOnMultipleQubits: if the provided
cirq.AbstractCircuit instance contains measurements applied on
several qubits at the same time.
"""
global_measurement_indices: list[dict[cirq.Qid, int]] = []
global_measurement_index: int = _measurement_offset
Expand All @@ -125,9 +144,53 @@ def _get_global_measurement_index(
for op in moment.operations:
if isinstance(op.gate, cirq.MeasurementGate):
if len(op.qubits) > 1:
raise MeasurementAppliedOnMultipleQubitsException(op.qubits)
raise TQECException(f"Found a measurement applied on multiple qubits ({op.qubits}).")
qubit: cirq.Qid = op.qubits[0]
global_measurement_indices[-1][qubit] = global_measurement_index
global_measurement_index += 1

return global_measurement_indices, global_measurement_index


def compute_global_measurements_lookback_offsets(
relative_measurements_record: RelativeMeasurementsRecord,
measurement_map: CircuitMeasurementMap,
current_moment_index: int,
) -> list[int]:
"""Computes, from the data in the given measurement_map, the global measurement offsets
This method uses the global data computed in the CircuitMeasurementMap instance given as
parameter to compute the measurement record indices for the current gate instance.
Args:
relative_measurements_record: the record of relative measurements to
compute global measurements offset from.
measurement_map: global measurement data obtained from the complete
quantum circuit.
current_moment_index: index of the moment this gate instance is found
in. Used to recover the correct data from the given measurement_map.
Returns:
the computed list of global measurements lookback offsets.
"""
global_measurements_lookback_offsets = []
origin = relative_measurements_record.origin
for relative_measurement in relative_measurements_record.data:
# Coordinate system: adding 2 GridQubit instances together, both are using the GridQubit
# coordinate system, so no issue here.
qubit = origin + relative_measurement.relative_qubit_positioning
relative_measurement_offset = relative_measurement.relative_measurement_offset
relative_offset = measurement_map.get_measurement_relative_offset(
current_moment_index,
qubit,
relative_measurement_offset,
)
if relative_offset is None:
raise TQECException(
"An error happened during the measurement offset lookback computation. "
f"You asked for the {relative_measurement_offset} measurement on {qubit} "
f"at the moment {current_moment_index}. The computed measurement map is"
f"{measurement_map}."
)
global_measurements_lookback_offsets.append(relative_offset)
return global_measurements_lookback_offsets
Loading

0 comments on commit 38e6d54

Please sign in to comment.