From 9e225f8309c3efd048634ec8e3f83bd9e6414390 Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:02:43 -0800 Subject: [PATCH 1/2] Fit a sine curve in the gradients tutorial MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The new example reuses the tutorial’s circuit and custom differentiator to fit a phase offset to a sine curve shifted by 0.2. It uses `tf.GradientTape` to differentiate the mean squared error with respect to the offset, then applies 30 gradient descent updates starting from zero. It prints the initial gradient, the loss before and after training, and the fitted offset. Refs #488 --- docs/tutorials/gradients.ipynb | 71 ++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/docs/tutorials/gradients.ipynb b/docs/tutorials/gradients.ipynb index 5bc5829b8..46399419a 100644 --- a/docs/tutorials/gradients.ipynb +++ b/docs/tutorials/gradients.ipynb @@ -783,6 +783,77 @@ "source": [ "Success: Now you can use all the differentiators that TensorFlow Quantum has to offer—and define your own." ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 5. Fit a sine curve\n", + "\n", + "The gradient of an expectation can drive a small regression. For the circuit above, $f(\\alpha) = ⟨Y(\\alpha)| X | Y(\\alpha)⟩ = \\sin(\\pi \\alpha)$. The expectation is a sine curve in the parameter $\\alpha$.\n", + "\n", + "Space inputs $x$ evenly over one period of that sine and use targets $\\sin(\\pi (x + 0.2))$. A scalar offset $\\phi$ starts at zero. Each input is evaluated at $\\alpha = x + \\phi$ on the same circuit and $X$ observable, using an analytic `tfq.layers.Expectation` built with a fresh `MyDifferentiator`. Reshape the predictions and the targets into matching vectors and take the mean squared error. `tf.GradientTape` propagates that loss gradient through the circuit to $\\phi$, and small gradient descent steps move $\\phi$ from $0$ toward $0.2$." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# A differentiator attaches to only one op, so make a fresh instance.\n", + "regression_dif = MyDifferentiator()\n", + "regression_expectation = tfq.layers.Expectation(differentiator=regression_dif)\n", + "\n", + "# Inputs x over one period of sin(pi * x). The period is 2.\n", + "x = np.linspace(0, 2, 20, endpoint=False)[:, np.newaxis].astype(np.float32)\n", + "targets = np.sin(np.pi * (x + 0.2)).astype(np.float32)\n", + "\n", + "# Scalar trainable offset. Running this cell again starts from zero.\n", + "offset = tf.Variable(0.0)\n", + "\n", + "\n", + "def regression_loss(phase_offset):\n", + " \"\"\"Mean squared error of the circuit against sin(pi * (x + 0.2)).\"\"\"\n", + " # Evaluate each input at alpha = x + offset.\n", + " alphas = tf.constant(x) + phase_offset\n", + " predictions = regression_expectation(my_circuit,\n", + " operators=pauli_x,\n", + " symbol_names=['alpha'],\n", + " symbol_values=alphas)\n", + " # Match shapes so each input contributes one residual.\n", + " predictions = tf.reshape(predictions, [-1])\n", + " target_vector = tf.reshape(targets, [-1])\n", + " return tf.reduce_mean(tf.square(predictions - target_vector))\n", + "\n", + "\n", + "learning_rate = 0.05\n", + "steps = 30\n", + "\n", + "with tf.GradientTape() as tape:\n", + " initial_loss = regression_loss(offset)\n", + "initial_gradient = tape.gradient(initial_loss, offset)\n", + "print('Initial loss: ', initial_loss.numpy())\n", + "print('Initial gradient:', initial_gradient.numpy())\n", + "\n", + "for _ in range(steps):\n", + " with tf.GradientTape() as tape:\n", + " loss = regression_loss(offset)\n", + " # Propagate the loss gradient through the circuit onto the offset.\n", + " gradient = tape.gradient(loss, offset)\n", + " offset.assign_sub(learning_rate * gradient)\n", + "\n", + "final_loss = regression_loss(offset)\n", + "print('Final loss: ', final_loss.numpy())\n", + "print('Fitted offset: ', offset.numpy())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The initial gradient is finite and nonzero, so the loss depends on the offset. Predictions and targets are vectors of the same length, which means the loss averages one squared residual per input. After the updates the loss is lower and the fitted offset is close to $0.2$. Running the cell again starts $\\phi$ at zero and reproduces the fit." + ] } ], "metadata": { From cfd93b4e42c2b9b1c22fb7c89584a552c86e7774 Mon Sep 17 00:00:00 2001 From: Matt Van Horn <455140+mvanhorn@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:48:59 -0800 Subject: [PATCH 2/2] Address review feedback --- docs/tutorials/gradients.ipynb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/docs/tutorials/gradients.ipynb b/docs/tutorials/gradients.ipynb index 46399419a..44cfcf652 100644 --- a/docs/tutorials/gradients.ipynb +++ b/docs/tutorials/gradients.ipynb @@ -809,6 +809,12 @@ "x = np.linspace(0, 2, 20, endpoint=False)[:, np.newaxis].astype(np.float32)\n", "targets = np.sin(np.pi * (x + 0.2)).astype(np.float32)\n", "\n", + "# Serialize the circuit and operator once outside the training loop.\n", + "x_tensor = tf.constant(x)\n", + "targets_tensor = tf.constant(targets)\n", + "circuit_tensor = tfq.convert_to_tensor([my_circuit] * len(x))\n", + "operator_tensor = tfq.convert_to_tensor([[pauli_x]] * len(x))\n", + "\n", "# Scalar trainable offset. Running this cell again starts from zero.\n", "offset = tf.Variable(0.0)\n", "\n", @@ -816,14 +822,14 @@ "def regression_loss(phase_offset):\n", " \"\"\"Mean squared error of the circuit against sin(pi * (x + 0.2)).\"\"\"\n", " # Evaluate each input at alpha = x + offset.\n", - " alphas = tf.constant(x) + phase_offset\n", - " predictions = regression_expectation(my_circuit,\n", - " operators=pauli_x,\n", + " alphas = x_tensor + phase_offset\n", + " predictions = regression_expectation(circuit_tensor,\n", + " operators=operator_tensor,\n", " symbol_names=['alpha'],\n", " symbol_values=alphas)\n", " # Match shapes so each input contributes one residual.\n", " predictions = tf.reshape(predictions, [-1])\n", - " target_vector = tf.reshape(targets, [-1])\n", + " target_vector = tf.reshape(targets_tensor, [-1])\n", " return tf.reduce_mean(tf.square(predictions - target_vector))\n", "\n", "\n",