diff --git a/docs/tutorials/gradients.ipynb b/docs/tutorials/gradients.ipynb index 5bc5829b8..44cfcf652 100644 --- a/docs/tutorials/gradients.ipynb +++ b/docs/tutorials/gradients.ipynb @@ -783,6 +783,83 @@ "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", + "# 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", + "\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 = 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_tensor, [-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": {