Swapping measurement keys with `with_measurement_key_mapping` collapses a `SympyCondition` (`a > b` becomes `a > a` or `b > b`)

Aperta
#8,529 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@EnesKartal ci sta già lavorando.

Dal 15/11/2026.

  • #8532 di @EnesKartal — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

The bug is in SympyCondition.replace_key in cirq-core/cirq/value/condition.py, which applies the key mapping one key at a time through subs. Start by reading that method, then run the snippet from the issue to see the condition change from a > b to a > a. Done means a swapped mapping keeps the condition's meaning, so the mapped run returns the same record as the unmapped circuit, and a regression test covers the swap case.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

triage/discuss

SympyCondition.replace_key substitutes one key at a time (self.expr.subs({str(current): sympy.Symbol(str(replacement))})), so a mapping that swaps two keys is applied sequentially and merges them.

import cirq, sympy
q0, q1, q2 = cirq.LineQubit.range(3)
a, b = sympy.symbols('a b')
c = cirq.Circuit(cirq.X(q0), cirq.measure(q0, key='a'), cirq.measure(q1, key='b'),
                 cirq.X(q2).with_classical_controls(cirq.SympyCondition(a > b)),
                 cirq.measure(q2, key='out'))
s = cirq.with_measurement_key_mapping(c, {'a': 'b', 'b': 'a'})
print(s)                                          # condition is now a > a or b > b (varies between runs)
print(cirq.Simulator().run(c).records['out'])     # [[[1]]]
print(cirq.Simulator().run(s).records['out'])     # [[[0]]], expected [[[1]]] (b=1 > a=0)

The measurements themselves are swapped correctly; only the condition is wrong, so the result changes silently. Which of a > a / b > b you get depends on the iteration order of the condition's keys, which can differ between interpreter runs (string hash randomization). Substituting all keys at once (subs(..., simultaneous=True) or a single xreplace of the full mapping) would avoid this.

Seen on 1.8.0.dev0; line unchanged on current main (cirq-core/cirq/value/condition.py). Different cause from #8525 and #8528, same family (condition objects under key rewriting).

Disclosure: found with AI-assisted differential testing that I direct; the snippet above was run before posting.

Saved-version clarification: a separate saved run on November 12, 2026 also reproduced this result with Cirq 1.8.0.dev20261111222613: the mapped condition was a > a, and the original/mapped outputs were [[[1]]] / [[[0]]]. That saved run is separate from the original 1.8.0.dev0 evidence.

Lingua principale
Python
Stelle
5.1k
Fork
1.3k
Merge medio
1g 16h
PR unite (30g)
41

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di quantumlib/Cirq

Tutte le issue di quantumlib/Cirq

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.