tensorflow / quantum

Hybrid Quantum-Classical Machine Learning in TensorFlow
https://www.tensorflow.org/quantum
Apache License 2.0
1.76k stars 558 forks source link

float64/complex128 support for tfq? #628

Open refraction-ray opened 2 years ago

refraction-ray commented 2 years ago

Does tfq currently support circuit simulation with float64/complex128 precision? At least the code demo below returns me a float32 tensor.

import tensorflow_quantum as tfq
import cirq
import numpy as np
import sympy

nwires = 10
nlayer = 6
qubits = [cirq.GridQubit(0,i) for i in range(nwires)]
symbols = sympy.symbols('params_0:'+str(nwires*nlayer))
symbol_values = [np.ones([nlayer*nwires], dtype=np.float64)]

circuit = cirq.Circuit()
for i in range(nwires):
    circuit.append(cirq.H(qubits[i]))
for j in range(nlayer):
    for i in range(nwires):
        circuit.append(cirq.rx(symbols[j*nwires+i])(qubits[i]))

oprs = [sum([cirq.Z(qubits[i])*cirq.Z(qubits[(i+1)%nwires]) for i in range(nwires)])]
ep = tfq.layers.Expectation(dtype=tf.float64)
ep(inputs=[circuit], symbol_names=symbols, symbol_values=symbol_values, operators=oprs)

I wonder if there is a way to enable complex128 simulation in tfq.

I believe float64 support is vital for variational quantum algorithms, especially for quantum simulations such as VQE. This is different from common machine learning setups where float32 is more than enough.

For example, the above program should return zero, while it actually gives <tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[2.9802322e-07]], dtype=float32)>. The error bar is float32-typical, and cannot be simply overlooked for physics problems.

zaqqwerty commented 2 years ago

We consider float32-typical error bars sufficient for quantum machine learning. This is because all real QPUs have errors far greater than this, and TFQ is targeted at development of algorithms meant to be run on such noisy processors. So we don't consider it "vital for variational quantum algorithms", at least not until the far fault-tolerant era. That said, maybe @MichaelBroughton can weigh in on the prospects for enabling such higher precision?

MichaelBroughton commented 2 years ago

That's correct @zaqqwerty . For most (if not all) of the NiSQ research we are doing float32 is more than enough precision. To add float64 support one would first need to look into adding it here:

https://github.com/quantumlib/qsim (for the SSE, AVX2, AVX512 and GPU sims) Then propogating it up to TFQ by registering new version of our ops using double precision.

refraction-ray commented 2 years ago

@zaqqwerty , @MichaelBroughton , Thanks for the reply and confirmation on the status of float64 support in tfq.

I agree with you that float32 is more than enough for tasks related with NISQ experiments. But from theoretical investigation perspective, the expressiveness of some parameterized circuits can indeed reach the accuracy in the order of 10^-6 or 10^-7, similar to the float32-typical error bar. Of course, I admit that these PQCs and accuracies are only possible for fault tolerant setups, but there are still good problems to look into for theorists anyhow. And a float64 enabled quantum circuit simulator is helpful in such cases.