`tfq_ps_decompose` corrupts `PhasedISwapPowGate` when `exponent` is numeric and `phase_exponent` is symbolic

Aperta Adatta ai principianti
#1,140 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

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

Direzione di ricerca

Leggi tensorflow_quantum/core/ops/tfq_ps_decompose_op.cc, in particolare getOpForISP e il relativo punto di chiamata a tfq_ps_decompose in TfqPsDecomposeOp::Compute. Confronta come gestisce gli esponenti simbolici e numerici con getOpForPXP, getOpForPISP e getOpForFSIM. Riproduci il problema con il circuito fornito e l’esempio di risoluzione dei parametri; il lavoro è completato quando le operazioni decomposte mantengono l’esponente numerico corretto e vengono risolte senza l’errore segnalato.

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

Descrizione

area/c++ area/core kind/bug-report

In TfqPsDecomposeOp::Compute (tensorflow_quantum/core/ops/tfq_ps_decompose_op.cc, lines 75–97), a PISP (cirq.PhasedISwapPowGate) operation is decomposed whenever either exponent or phase_exponent is symbolic (Arg::ArgCase::kSymbol):

if (exponent.arg_case() == Arg::ArgCase::kSymbol ||
    phase_exponent.arg_case() == Arg::ArgCase::kSymbol) {
  // Decompose cirq.PhasedISwapPowGate only if it is
  // parameterized.
  num_extra_moments = 5;
  Operation new_op;

  new_op = getOpForPISP(cur_op, 0, 0);
  cur_moment.mutable_operations()->at(k) = new_op;
  new_op = getOpForPISP(cur_op, 1, 1);
  *temp_moment_list[0].add_operations() = new_op;
  new_op = getOpForISP(cur_op, "XXP", exponent.symbol());
  *temp_moment_list[1].add_operations() = new_op;
  new_op = getOpForISP(cur_op, "YYP", exponent.symbol());
  *temp_moment_list[2].add_operations() = new_op;
  new_op = getOpForPISP(cur_op, 1, 0);
  *temp_moment_list[3].add_operations() = new_op;
  new_op = getOpForPISP(cur_op, 0, 1);
  *temp_moment_list[4].add_operations() = new_op;
}

However, getOpForISP (lines 164–187) was written for ISP, where exponent is guaranteed to be symbolic, and unconditionally writes new_op_map["exponent"].set_symbol(symbol).

When phase_exponent is a sympy.Symbol and exponent is a numeric constant (Arg::ArgCase::kArgValue — including Cirq's default exponent=1.0, or any non-unit float such as 0.5):

  1. exponent.symbol() evaluates to "" (empty string), and exponent.arg_value().float_value() is ignored.
  2. The decomposed XXP and YYP operations receive a symbolic exponent with empty symbol name "" (sympy.Symbol('') after deserialization) instead of a numeric float -0.5 * exponent.arg_value().float_value().
  3. Resolving circuit parameters leaves unresolved sympy.Symbol('') expressions on the XXPowGate and YYPowGate operations, corrupting the decomposed circuit and breaking ParameterShift differentiation whenever PhasedISwapPowGate has a symbolic phase_exponent and numeric exponent.
Minimal Reproduction
import cirq
import sympy
import tensorflow_quantum as tfq
from tensorflow_quantum.core.ops import tfq_ps_util_ops

q0, q1 = cirq.GridQubit.rect(1, 2)
beta = sympy.Symbol("beta")

# Numeric exponent with symbolic phase_exponent.
circuit = cirq.Circuit(
    cirq.PhasedISwapPowGate(phase_exponent=beta, exponent=0.5).on(q0, q1)
)

decomposed_tensor = tfq_ps_util_ops.tfq_ps_decompose(
    tfq.convert_to_tensor([circuit])
)
decomposed_circuit = tfq.from_tensor(decomposed_tensor)[0]
print(decomposed_circuit)

# Raises TypeError because XXPowGate and YYPowGate contain sympy.Symbol('').
cirq.unitary(cirq.resolve_parameters(decomposed_circuit, {"beta": 0.72}))

Observed decomposed circuit:

(0, 0): ───Z^beta───────────────XX───────────YY───────────Z^(-beta)────────────
                                │            │
(0, 1): ────────────Z^(-beta)───XX^(-0.5*)───YY^(-0.5*)───────────────Z^beta───
Possible Fix

Update getOpForISP to read exponent from cur_op directly (or accept const Arg& instead of std::string symbol) and switch on exponent.arg_case(), consistent with getOpForPXP, getOpForPISP, and getOpForFSIM: keep set_symbol(...) and exponent_scalar = -0.5 * cur_exponent_scalar for Arg::ArgCase::kSymbol, and set exponent to -0.5 * exponent.arg_value().float_value() with exponent_scalar = 1.0 for Arg::ArgCase::kArgValue.

Lingua principale
Python
Stelle
2.2k
Fork
674
Merge medio
1g 14h
PR unite (30g)
5

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 tensorflow/quantum

Tutte le issue di tensorflow/quantum

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.