Feature: Add Magic Square game for Contextuality demonstration#448
Feature: Add Magic Square game for Contextuality demonstration#448alejogoogle wants to merge 22 commits intoquantumlib:mainfrom
Conversation
|
In addition to addressing Doug's comments about the code, I think also it's important to add a description to the pull request. Here are some points to address:
I realize it's more work to write a description, and I'm sorry for that. Perhaps an AI tool can help with this in some way. |
mhucka
left a comment
There was a problem hiding this comment.
A number of these items are nit-picky details, and I tried to make it easy to accept or reject the changes using GitHub's interface for suggested edits. Hopefully that'll make this easier.
| cirq.H.on(q[1]), | ||
| cirq.M(q[1], key="alice_datas"), | ||
| ]) | ||
|
|
There was a problem hiding this comment.
The code above assigns values conditionally, based on mermin_row and mermin_col, but never tests that the values passed to the function are within the bounds assumed by the if/elif clauses. If an invalid value is passed (e.g., mermin_row = 3), or if sub_case is "square_2", the function will fail with an UnboundLocalError when it tries to return alice_circuit + bob_circuit. An else block should be added that raises a ValueError for invalid/unsupported inputs.
| return self._generate_choices_from_rules_measure_3rd_classical_multiplication() | ||
| if game == "measure_3rd_quantum_multiplication": | ||
| return self._generate_choices_from_rules_measure_3rd_quantum_multiplication() | ||
|
|
There was a problem hiding this comment.
In the code above, if game does not match any of the 3 values tested, this method implicitly returns None. This violates the tuple[np.ndarray, np.ndarray] return type hint. There should be explicit else to raise ValueError(f"Unknown game: {game}") or raise NotImplementedError at the end of the method.
| alice_choices, bob_choices = self.generate_choices(game) | ||
| agree_given_multiply_matrix = np.zeros((3, 3)) | ||
| repetitions = alice_choices.shape[2] | ||
| print(f"{repetitions=}") |
There was a problem hiding this comment.
This looks like left-over debugging code. If so, it would be better to remove it.
| alice_choices, bob_choices = self.generate_choices(game) | ||
| multiply_matrix = np.zeros((3, 3)) | ||
| repetitions = alice_choices.shape[2] | ||
| print(f"{repetitions=}") |
There was a problem hiding this comment.
This looks like left-over debugging code. If so, it would be better to remove it.
| alice_choices, bob_choices = self.generate_choices(game) | ||
| agree_matrix = np.zeros((3, 3)) | ||
| repetitions = alice_choices.shape[2] | ||
| print(f"{repetitions=}") |
There was a problem hiding this comment.
This looks like left-over debugging code. If so, it would be better to remove it.
Co-authored-by: Michael Hucka <mhucka@google.com>
Co-authored-by: Michael Hucka <mhucka@google.com>
Co-authored-by: Michael Hucka <mhucka@google.com>
Co-authored-by: Michael Hucka <mhucka@google.com>
Co-authored-by: Michael Hucka <mhucka@google.com>
This PR adds a new experimental module to the ReCirq repository that implements the Mermin-Peres Magic Square game (a form of quantum pseudo-telepathy).
Specifically, this code:
Adds new functionality: It provides a framework to run the game using cirq on both simulators and quantum hardware.
Implements three experimental variations:
infer_3rd: Measuring two observables and calculating the third classically.
measure_3rd_classical_multiplication: Measuring components and multiplying the outcomes.
measure_3rd_quantum_multiplication: Using two-qubit interactions to directly measure the product observable.
Provides analysis tools: Includes a ContextualityResult class to calculate win rates, agreement matrices, and parity check fractions to verify quantum non-locality.
Relevance to Contextuality Work
The Magic Square game is a cornerstone "warm-up" experiment for demonstrating quantum contextuality (https://arxiv.org/pdf/2512.02284). It proves that measurement outcomes cannot be pre-determined independently of their "context" (which other measurements are performed simultaneously).
No new external libraries are required.