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

Abierto Apto para principiantes
#1,140 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
78/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python, tensorflow

Línea de trabajo

Lee tensorflow_quantum/core/ops/tfq_ps_decompose_op.cc, especialmente getOpForISP y su punto de llamada a tfq_ps_decompose en TfqPsDecomposeOp::Compute. Compara cómo maneja los exponentes simbólicos y numéricos con getOpForPXP, getOpForPISP y getOpForFSIM. Reproduce el problema con el circuito proporcionado y el ejemplo de resolución de parámetros; se considera terminado cuando las operaciones descompuestas conservan el exponente numérico correcto y se resuelven sin el error indicado.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Python
Estrellas
2.2k
Forks
674
Merge medio
1 d 14 h
PR fusionados (30 d)
5

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de tensorflow/quantum

Todos los issues de tensorflow/quantum

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.