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

未关闭 适合新手
#1,140 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python, tensorflow

调研方向

阅读 tensorflow_quantum/core/ops/tfq_ps_decompose_op.cc,尤其是 getOpForISP 以及它在 TfqPsDecomposeOp::Compute 中调用 tfq_ps_decompose 的位置。对比它与 getOpForPXP、getOpForPISP 和 getOpForFSIM 处理符号指数和数值指数的方式。使用提供的电路和参数解析示例复现问题;当分解后的操作保留正确的数值指数,并且在解析时不再出现所报告的错误,即表示完成。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Python
星标
2.2k
派生
674
平均合并
1 天 14 小时
30 天内合并 PR
5

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

tensorflow/quantum 的其他 Issue

查看 tensorflow/quantum 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。