-
Notifications
You must be signed in to change notification settings - Fork 585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Discussion] Adding quantum math features #380
Comments
|
To add to my comments in the TFQ sync meeting about how useful a This is what I had to do in TensorFlow 1.x to calculate the Quantum Natural Gradient: def QuantumNaturalGradient(state, loss, vrs, optimizer=None, stability_shift=None,
learning_rate: float = 0.01):
"""
Implementation of the quantum natural gradient gradient method
Args:
*state (Tensor)*:
Output state of the circuit.
*loss (Tensor)*:
Loss function that uses the provided state.
*vrs (Variables)*:
Variables to be optimized.
*optimizer (Tensorflow Optimizer)*:
Tensorflow optimizer from the tf.train API
Returns (Operation):
Train step operation.
"""
if optimizer == None:
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
assign_ops = []
for variable in vrs:
variable = [variable]
jac, nparams = prepGeometricTensor(state, variable)
grads = tf.gradients(loss, variable)
sf_metric = []
# fubini-study metric
for i in range(nparams):
for j in range(nparams):
part_1 = tf.math.conj(tf.reshape(jac[i], (1, -1))) @ tf.reshape(jac[j], (-1, 1))
part_2 = tf.math.conj(tf.reshape(jac[i], (1, -1))) @ tf.reshape(state, (-1, 1)) + \
tf.math.conj(tf.reshape(state, (1, -1))) @ tf.reshape(jac[j], (-1, 1))
sf_metric.append(part_1 - part_2)
eta = tf.math.real(tf.reshape(tf.stack(sf_metric), (nparams, nparams)))
# QNG
grads = tf.stack(grads)
if stability_shift is not None:
eta += tf.eye(*eta.shape.as_list()) * stability_shift
grads = tf.linalg.solve(eta, tf.reshape(grads, (-1, 1)))
if len(variable) == 1:
grads = tf.reshape(grads, (variable[0].shape))
assign_ops.append(optimizer.apply_gradients(zip([grads], variable)))
else:
grads = tf.split(grads, nparams, axis=0)
assign_ops.append(optimizer.apply_gradients(zip(grads, variable)))
return assign_ops It resulted in a huge compute graph that was crazy slow and unstable since inverting eta can be ill-defined. However, for smaller systems this worked and sped up the optimization of a variational circuit significantly (as in https://arxiv.org/abs/1909.02108). If you leave out part_2 of the FS metric, you get Imaginary Time Evolution: https://arxiv.org/abs/1804.03023. Having an efficient, parallelizable |
Since our discussions we have had a number of people also request the fubini study metric to explore the natural gradient. Perhaps once we make the op we should also make a |
Was also thinking this might be a good place to add MPS based simulation algorithms with new ops like:
|
Hey can I work on this? I just want to summarize these are the math ops we want to implement ( I have ordered in terms of increasing difficulty, or at least thats what I think).
|
Absolutely go for it! |
Is there any update regarding its status? I also wanted to work on natural and hessian gradients. |
I think this is something @jaeyoo might be interested in taking on. If he is we should probably break some of these ops off into their own issues and assign people ownership, just so we know who's working on what. |
Hi @nishant34 I am already working on natural gradients. Would you mind if you are working on natural gradients optimizer or hessian gradients? |
Sorry was busy with other stuff, will start working on this now. How do you want me to break this up into multiple issues? |
I'd say a good approach would be this:
Since op contributions aren't coming in very quickly and are a fairly sizable undertaking, I think anyone looking to contribute a new op should definitely take this "one at a time issue creation followed by PR chain" approach. |
Similar to MPS simulation we might want to also support clifford circuit simulation. Likely built on top of: https://github.com/quantumlib/Stim. Since expectation values aren't as useful in clifford simulation we'd probably only need:
|
We currently have MPS simulator functionality in cirq wondering if I could leverage that for MPS related OPS instead of doing any C++. |
That's an interesting idea and using python only can in theory work (like we did |
Gotcha, just checking. Will get started with the C++ implementation. Will follow the steps you listed above. Thanks. |
Sorry, I was unwell and had to take a couple of months rest, just checking in to say I am still pursuing this. |
Sometimes, it is useful to be able to analyze circuits in ways that aren't valid on a quantum computer, but might still be interesting for theory reasons. I came across this issue a little while ago when wanting to compute the overlap
<psi_1 | psi_2>
of a state and had a hard time keeping memory limits under control with larger batch and state sizes usingtfq.layers.State
. I ultimately ended up simulating the circuits one by one, and doing the inner products in tf manually. It worked, but it wasn't pretty.Would people be interested in implementing things like:
Examples that can be done right now, but are tricky to get right without blowing up memory:
Other interesting math functions might be worth having
Does anyone else have any suggestions for useful math function ? @dabacon @zaqqwerty @jaeyoo @we-taper
Final thought: If we do end up making this
.math
module , maybe we should movecalculate_unitary
andcalculate_state
inside of it.The text was updated successfully, but these errors were encountered: