Hessian calculation fails using tf.jacobian
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 35/100
- Tipo de issue
- Error
- Claridad
- Bastante claro
- Estado de actividad
- Estancado
- Stack tecnológico
- python, tensorflow
- Área
- machine-learning
Línea de trabajo
Comienza con el ejemplo de GradientTape anidado y la llamada a tfq.layers.Expectation del issue; después, reproduce el LookupError para TfqAdjointGradient siguiendo el patrón avanzado de autodiff de TensorFlow. Rastrea el punto de entrada del diferenciador implicado y define como terminado obtener la matriz hessiana sin el fallo por gradiente ausente, con cobertura para el caso de circuito parametrizado reportado.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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!
- Lenguaje dominante
- Python
- Estrellas
- 2.2k
- Forks
- 665
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de tensorflow/quantum
-
area/devops help wanted no QC knowledge needed priority/before-0.7.7
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
tensorflow/quantum#1082 · 1 comentario · 1 reacción ·
-
Support Keras 3Quizá libre de nuevo @rosspeili la tomó hace 74 días y no hay ningún pull request abierto. Abiertoarea/keras help wanted skill-level/intermediate
tensorflow/quantum#1090 · 4 comentarios · 1 asignado ·
-
Investigate support for JAXAbiertoskill-level/expert
Dificultad 5/5 Más de una semana Aptitud para principiantes 30/100
tensorflow/quantum#1089 · 2 comentarios ·
-
area/core priority/after-0.7.7 skill-level/expert
Dificultad 5/5 Más de una semana Aptitud para principiantes 25/100
tensorflow/quantum#1088 ·
-
Support dynamic quantum circuitsAbiertoarea/circuits priority/after-0.7.7 skill-level/expert
Dificultad 5/5 Más de una semana Aptitud para principiantes 25/100
tensorflow/quantum#1087 ·
Todos los issues de tensorflow/quantum
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-2 días Aptitud para principiantes 70/100
-
FingerprintSplitter raises ZeroDivisionError when int(frac_train * len(dataset)) floors to zeroAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
Los mantenedores suelen responder en 7 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
lmstudio-ai/mlx-engine#376 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
pyiron/bagofholding#166 ·