Hessian calculation fails using tf.jacobian
维护者通常 4 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 35/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- python, tensorflow
调研方向
从 issue 中嵌套的 GradientTape 示例和 tfq.layers.Expectation 调用开始,然后按照 TensorFlow 的高级 autodiff 模式重现 TfqAdjointGradient 的 LookupError。跟踪所涉及的 differentiator 入口点,并将“在不发生缺少梯度的失败的情况下获得 Hessian,同时为报告的参数化电路案例提供覆盖”定义为完成标准。
由索引模型根据 Issue 内容生成。
描述
Python: 3.8
TFQ: 0.4.0
Hi,
I am trying to get some Hessians from the one of my parameterized quantum circuits. This trainstep works as intended:
def build_train_step(circuit: cirq.Circuit, symbols: List,
paulisum: List[cirq.PauliSum], learning_rate: float) -> \
Tuple[Any, tf.Variable, tfq.layers.Expectation]:
model_params = tf.Variable(tf.random.uniform([1, len(symbols)]) * 2,
constraint=lambda x: tf.clip_by_value(x, 0, 4))
expectation_layer = tfq.layers.Expectation()
optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
@tf.function
def train_step():
with tf.GradientTape() as tape:
expectation_batch = expectation_layer(circuit,
symbol_names=symbols,
symbol_values=model_params,
operators=paulisum)
energy = tf.reduce_sum(expectation_batch)
gradients = tape.gradient(energy, model_params)
optimizer.apply_gradients(zip([gradients], [model_params]))
return energy
return train_step, model_params, expectation_layer
Following this example in the TensorFlow 2 docs I was hoping I could get the Hessian with the following code:
def build_train_step_hessians(circuit: cirq.Circuit, symbols: List,
paulisum: List[cirq.PauliSum], learning_rate: float) -> \
Tuple[Any, tf.Variable, tfq.layers.Expectation]:
model_params = tf.Variable(tf.random.uniform([1, len(symbols)]) * 2,
constraint=lambda x: tf.clip_by_value(x, 0, 4))
expectation_layer = tfq.layers.Expectation()
optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
@tf.function
def train_step():
with tf.GradientTape() as t2:
with tf.GradientTape() as t1:
expectation_batch = expectation_layer(circuit,
symbol_names=symbols,
symbol_values=model_params,
operators=paulisum)
energy = tf.reduce_sum(expectation_batch)
gradients = t1.gradient(energy, model_params)
hess = t2.jacobian(gradients, model_params)
optimizer.apply_gradients(zip([gradients], [model_params]))
return energy, hess
return train_step, model_params, expectation_layer
But this throws the error:
...
LookupError: No gradient defined for operation 'TfqAdjointGradient' (op type: TfqAdjointGradient)
From which I conclude that calculating gradients of gradients is not supported yet ( I tried the other differentiators as well). Am I out of luck here? Or is there a hack I can use to get the Hessians from the circuit? Thanks! If you need an example where I use this train step I can throw one together.
P.S.
I am in the process of rewriting all my research code to TFQ and so far everything has worked like a charm. No more super slow graph building times and worrying about how to extract stuff the graph with my own TF1 simulator. And the adjoint differentiator in TFQ is amazing as well; I ran a VQE optimization with like 500 parameters the other day without any issues. Great stuff!
- 主要语言
- Python
- 星标
- 2.2k
- 派生
- 672
- 平均合并
- 3 天 5 小时
- 30 天内合并 PR
- 4
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
tensorflow/quantum 的其他 Issue
-
area/devops help wanted no QC knowledge needed priority/before-0.7.7
难度 2/5 1-3 小时 新手友好度 68/100
tensorflow/quantum#1082 · 2 条评论 · 1 个 reaction ·
维护者通常 4 天内回复
-
Support Keras 3可能重新可做 @rosspeili 于 95 天前认领,目前没有进行中的 PR。 未关闭area/keras help wanted skill-level/intermediate
tensorflow/quantum#1090 · 5 条评论 · 已指派 1 人 ·
维护者通常 4 天内回复
-
area/circuits priority/after-0.7.7 skill-level/expert
难度 5/5 一周以上 新手友好度 25/100
tensorflow/quantum#1087 ·
维护者通常 4 天内回复
-
Efficiently handle circuit sizes 10x or more of today's circuits可能重新可做 @SripriyaDevarapalli 于 15 天前认领,目前没有进行中的 PR。 未关闭area/circuits area/performance help wanted priority/after-0.7.7 skill-level/expert
难度 5/5 一周以上 新手友好度 25/100
tensorflow/quantum#1086 · 1 条评论 ·
维护者通常 4 天内回复
-
area/health no QC knowledge needed priority/after-0.7.7
难度 3/5 1-2 天 新手友好度 55/100
tensorflow/quantum#1083 · 1 条评论 ·
维护者通常 4 天内回复
查看 tensorflow/quantum 的全部 Issue
相似的 Issue
-
Pull-through from gcr.io returns 500 when pulling using podman/buildah可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭Issue Triage-Needed
难度 2/5 1-3 小时 新手友好度 68/100
pulp/pulp_container#2581 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 85/100
521xueweihan/HelloGitHub#4040 ·
-
bug
难度 2/5 1-3 小时 新手友好度 64/100
ClanGenOfficial/clangen#6313 ·
维护者通常 2 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 84/100
维护者通常 1 天内回复
-
bug(trainer): CustomTrainer func_args with non literal values fail inside the TrainJob可能已有人在做 @avanishkasar 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复