Skip to content

Feature: Add Magic Square game for Contextuality demonstration#448

Open
alejogoogle wants to merge 22 commits intoquantumlib:mainfrom
alejogoogle:mermin-peres-game
Open

Feature: Add Magic Square game for Contextuality demonstration#448
alejogoogle wants to merge 22 commits intoquantumlib:mainfrom
alejogoogle:mermin-peres-game

Conversation

@alejogoogle
Copy link
Copy Markdown
Contributor

@alejogoogle alejogoogle commented Dec 17, 2025

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:

    1. infer_3rd: Measuring two observables and calculating the third classically.

    2. measure_3rd_classical_multiplication: Measuring components and multiplying the outcomes.

    3. 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.

@dstrain115 dstrain115 self-requested a review December 17, 2025 00:37
@mhucka
Copy link
Copy Markdown
Collaborator

mhucka commented Jan 15, 2026

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:

  • What is this PR for? What does it actually do? From the title, one can infer it concerns a game, but that's about it – the title doesn't say if this PR adds code for a game, or fixes a problem with exiting code, or something else. (We shouldn't assume that the reader of the PR will take time to read the code in order to deduce these things.)

  • What is the relevance to the contextuality work? Is this game mentioned in the paper, or is it just a fun supplement, or something else? (We shouln't assume that the reader will go look at the paper to figure that out.)

  • Is this related to any other PR or issue in the ReCirq repo?

  • Does this game require new libraries that aren't already in the project's requirements?

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.

@alejogoogle alejogoogle changed the title Contextuality: MagicSquare game Feature: Add Magic Square game for Contextuality demonstration Feb 20, 2026
Copy link
Copy Markdown
Collaborator

@mhucka mhucka left a comment

Choose a reason for hiding this comment

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

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"),
])

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.

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()

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.

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=}")
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.

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=}")
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.

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=}")
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.

This looks like left-over debugging code. If so, it would be better to remove it.

alejogoogle and others added 5 commits March 23, 2026 11:17
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants