Document how to use TFQ on physical or simulated chips
I am attempting to use TFQ to do measurement noise mitigation by passing the measured bitstrings into a tensorflow model. Ultimately, this is something I'd like to run on either physical or simulated hardware, but I don't see support for this currently in TFQ.
I have been reading in the source for Expectation, Sample, and SampledExpectation, and from what I can tell there doesn't seem to be support for physical or cirq.Simulator.run() type backends through those channels. Currently it seems like the only options are "noiseless" and "noisy", without the ability to pass a customized noise model or on-chip device. Is there a way to pass a physical device execution or a simulated device to generate the data to compute expectation values with respect to as TFQ layer?
Thanks.
Hi @kharazity. Thanks for raising the issue.
Ultimately, this is something I'd like to run on either physical or simulated hardware, but I don't see support for this currently in TFQ.
Supporting "simulated hardware" is something you can do in TensorFlow Quantum. Typically when trying to simulate real hardware, your goal is to come up with some noise model that you believe will accurately capture the noisy behavior of the device you care about. In Cirq and TFQ you can do this using the various types of built in Cirq Channels (cirq.depolarize, cirq.amplitude_damp, cirq.bit_flip etc.). See this:
import cirq
import tensorflow_quantum as tfq
def my_device_noise(circuit):
"""Add channels to the circuit however you like."""
depolarized_circiut = circuit.with_noise(cirq.depolarize(0.001))
full_noisy_circuit = depolarized_circiut + cirq.Circuit(cirq.bit_flip(0.1).on_each(circuit.all_qubits()))
return full_noisy_circuit
# Pick some qubits and build a GHZ circuit.
qubits = cirq.GridQubit.rect(1, 10)
my_ghz = cirq.Circuit(
cirq.H(qubits[0]),
(cirq.CNOT(q1, q2) for q1, q2 in zip(qubits, qubits[1:]))
)
# Make a noisy version based on what I believe to be representative device noise.
my_noisy_ghz = my_device_noise(my_ghz)
# Sample from noiseless circuit.
print(tfq.layers.Sample(backend='noiseless')(my_ghz, repetitions=10).to_tensor())
# Sample from noisy circuit.
print(tfq.layers.Sample(backend='noisy')(my_noisy_ghz, repetitions=10).to_tensor())
Here I could swap out to tfq.layers.Expectation in order to do expectation in both the noisy + noiseless settings and use these layers inside arbitrary compute graphs. A worked example where this is done can be found here: https://www.tensorflow.org/quantum/tutorials/noise
It is worth noting that neither Cirq nor TFQ will give you a "representative device noise model" that is something you need to come up with on your own or via on of the cirq vendor packages like cirq-google(https://quantumai.google/reference/python/cirq_google/experimental/noise_models) or from doing your own investigation.
You can pass a custom backend=cirq.ArbitrarySamplerType() to most of the TFQ Keras layers (in order to make use of custom simulators or actual chips), but it is a discouraged code path that is often times covered by just using backend=noisy/noiseless.
Does this help clear things up ?
Any updates on this issue or can it be closed?