Skip to content

Commit 43220c9

Browse files
committed
Add docstrings
1 parent e4a1a61 commit 43220c9

File tree

1 file changed

+63
-3
lines changed

1 file changed

+63
-3
lines changed

recirq/contextuality/kochen_specker.py

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,18 @@
3939

4040
@attrs.frozen
4141
class MagicSquare:
42-
"""A Mermin-Peres magic square."""
42+
"""Represents a MagicSquare.
43+
44+
A MagicSquare is a 3x3 grid of two-qubit Pauli operators used to demonstrate quantum
45+
contextuality, specifically the Kochen-Specker theorem.
46+
47+
The operators in each row and each column mutually commute.
48+
The product of operators in each row is +I.
49+
The product of operators in each column is -I.
50+
51+
Attributes:
52+
paulis: A 3x3 sequence of strings representing the Pauli operators in the square.
53+
"""
4354

4455
paulis: Sequence[Sequence[str]]
4556

@@ -102,9 +113,14 @@ def kochen_specker_expt(
102113
qnd_strategy: QndStrategy = "map_all_paulis_to_ancilla",
103114
virtual_z_half_turns: float = 0.0,
104115
) -> MagicSquareExperiment:
105-
"""Builds a Kochen-Specker experiment.
116+
"""Builds a Kochen-Specker experiment, which measures state-independent contextuality.
117+
118+
See https://arxiv.org/pdf/2512.02284 for more details.
106119
107120
Args:
121+
q0: First data qubit.
122+
q1: Second data qubit.
123+
anc: Ancilla qubit.
108124
num_contexts: Number of rows+cols measured out of the 6
109125
possible row/col.
110126
seed: Seed for random number generator. Specifying this will give
@@ -114,6 +130,9 @@ def kochen_specker_expt(
114130
amount of half turns around the Z axis to apply on the ancilla during the mapping of
115131
the paulis from the data qubits (q0, q1). This virtualZ is applied before the last
116132
Hadamard during the mapping of the Pauli to the ancilla.
133+
134+
Returns:
135+
A `MagicSquareExperiment` object containing the constructed circuit and context info.
117136
"""
118137

119138
axes: list[Axis] = ["col", "row"]
@@ -178,23 +197,64 @@ def _copy_circuit(circuit: cirq.FrozenCircuit) -> cirq.FrozenCircuit:
178197

179198
@attrs.frozen
180199
class MagicSquareExperiment:
200+
"""An experiment to test for contextuality using a magic square.
201+
202+
Attributes:
203+
square: The `MagicSquare` instance used for the experiment.
204+
circuit: The `cirq.FrozenCircuit` that will be run. This circuit
205+
is composed of measurements for a sequence of randomly chosen
206+
contexts (rows or columns of the magic square).
207+
contexts: A dictionary mapping each possible context (row or column) to
208+
the sequence of `PauliKey`s that identify the measurements for that
209+
context.
210+
"""
211+
181212
square: MagicSquare
182213
circuit: cirq.FrozenCircuit
183214
contexts: dict[tuple[Axis, int], Sequence[PauliKey]]
184215

185216
def run(self, sampler: cirq.Sampler, repetitions: int = 1000) -> MagicSquareResult:
217+
"""Runs the experiment circuit on a sampler.
218+
219+
Args:
220+
sampler: The `cirq.Sampler` to run the experiment on.
221+
repetitions: The number of times to repeat the circuit.
222+
223+
Returns:
224+
A `MagicSquareResult` object containing the results of the experiment.
225+
"""
186226
result = sampler.run(self.circuit, repetitions=repetitions)
187227
return MagicSquareResult(self, result)
188228

189229

190230
@attrs.frozen
191231
class PauliKey:
232+
"""A key to identify a specific Pauli measurement in a circuit.
233+
234+
Attributes:
235+
key: The measurement key string for the Pauli measurement.
236+
slice: A slice or list of indices to select specific measurement instances
237+
if the key appears multiple times in a circuit for a given context.
238+
Defaults to `slice(None)` to select all instances.
239+
"""
240+
192241
key: str
193242
slice: list[int] | slice = slice(None)
194243

195244

196245
@attrs.frozen(slots=False)
197246
class MagicSquareResult:
247+
"""The results of a `MagicSquareExperiment`.
248+
249+
This class holds the `cirq.Result` from running the experiment and provides
250+
methods to analyze the results, such as estimating the Kochen-Specker value (chi)
251+
and other statistical properties.
252+
253+
Attributes:
254+
expt: The `MagicSquareExperiment` that was run.
255+
result: The `cirq.Result` object from the sampler.
256+
"""
257+
198258
expt: MagicSquareExperiment
199259
result: cirq.Result
200260

@@ -790,7 +850,7 @@ def estimate_parities(
790850
records: The value for each key is a 2-D array of booleans, with the first index running
791851
over circuit repetitions, the second index running over instances of the measurement
792852
key in the circuit.
793-
contexts: # TODO
853+
contexts: Mapping contexts to their corresponding pauli keys.
794854
795855
Returns:
796856
context_averages: A dictionary mapping a context (axis, idx) to a tuple

0 commit comments

Comments
 (0)