Path: blob/master/site/en-snapshot/quantum/tutorials/noise.ipynb
25118 views
Copyright 2020 The TensorFlow Authors.
Noise
Noise is present in modern day quantum computers. Qubits are susceptible to interference from the surrounding environment, imperfect fabrication, TLS and sometimes even gamma rays. Until large scale error correction is reached, the algorithms of today must be able to remain functional in the presence of noise. This makes testing algorithms under noise an important step for validating quantum algorithms / models will function on the quantum computers of today.
In this tutorial you will explore the basics of noisy circuit simulation in TFQ via the high level tfq.layers
API.
Setup
1. Understanding quantum noise
1.1 Basic circuit noise
Noise on a quantum computer impacts the bitstring samples you are able to measure from it. One intuitive way you can start to think about this is that a noisy quantum computer will "insert", "delete" or "replace" gates in random places like the diagram below:
Building off of this intuition, when dealing with noise, you are no longer using a single pure state but instead dealing with an ensemble of all possible noisy realizations of your desired circuit: . Where gives the probability that the system is in .
Revisiting the above picture, if we knew beforehand that 90% of the time our system executed perfectly, or errored 10% of the time with just this one mode of failure, then our ensemble would be:
If there was more than just one way that our circuit could error, then the ensemble would contain more than just two terms (one for each new noisy realization that could happen). is referred to as the density matrix describing your noisy system.
1.2 Using channels to model circuit noise
Unfortunately in practice it's nearly impossible to know all the ways your circuit might error and their exact probabilities. A simplifying assumption you can make is that after each operation in your circuit there is some kind of channel that roughly captures how that operation might error. You can quickly create a circuit with some noise:
You can examine the noiseless density matrix with:
And the noisy density matrix with:
Comparing the two different 's you can see that the noise has impacted the amplitudes of the state (and consequently sampling probabilities). In the noiseless case you would always expect to sample the state. But in the noisy state there is now a nonzero probability of sampling or or as well:
Without any noise you will always get :
If you increase the noise a little further it will become harder and harder to distinguish the desired behavior (sampling ) from the noise:
Note: Try experimenting with different channels in your circuit to generate noise. Common channels supported in both Cirq and TFQ can be found here
2. Basic noise in TFQ
With this understanding of how noise can impact circuit execution, you can explore how noise works in TFQ. TensorFlow Quantum uses monte-carlo / trajectory based simulation as an alternative to density matrix simulation. This is because the memory complexity of density matrix simulation limits large simulations to being <= 20 qubits with traditional full density matrix simulation methods. Monte-carlo / trajectory trades this cost in memory for additional cost in time. The backend='noisy'
option available to all tfq.layers.Sample
, tfq.layers.SampledExpectation
and tfq.layers.Expectation
(In the case of Expectation
this does add a required repetitions
parameter).
2.1 Noisy sampling in TFQ
To recreate the above plots using TFQ and trajectory simulation you can use tfq.layers.Sample
2.2 Noisy sample based expectation
To do noisy sample based expectation calculation you can use tfq.layers.SampleExpectation
:
Compute the noiseless expectation estimates via sampling from the circuit:
Compare those with the noisy versions:
You can see that the noise has particularly impacted the accuracy, with my_really_noisy_circuit
concentrating very quickly towards 0.
2.3 Noisy analytic expectation calculation
Doing noisy analytic expectation calculations is nearly identical to above:
3. Hybrid models and quantum data noise
Now that you have implemented some noisy circuit simulations in TFQ, you can experiment with how noise impacts quantum and hybrid quantum classical models, by comparing and contrasting their noisy vs noiseless performance. A good first check to see if a model or algorithm is robust to noise is to test under a circuit wide depolarizing model which looks something like this:
Where each time slice of the circuit (sometimes referred to as moment) has a depolarizing channel appended after each gate operation in that time slice. The depolarizing channel with apply one of with probability or apply nothing (keep the original operation) with probability .
3.1 Data
For this example you can use some prepared circuits in the tfq.datasets
module as training data:
Writing a small helper function will help to generate the data for the noisy vs noiseless case:
3.2 Define a model circuit
Now that you have quantum data in the form of circuits, you will need a circuit to model this data, like with the data you can write a helper function to generate this circuit optionally containing noise:
3.3 Model building and training
With your data and model circuit built, the final helper function you will need is one that can assemble both a noisy or a noiseless hybrid quantum tf.keras.Model
:
4. Compare performance
4.1 Noiseless baseline
With your data generation and model building code, you can now compare and contrast model performance in the noiseless and noisy settings, first you can run a reference noiseless training:
And explore the results and accuracy:
4.2 Noisy comparison
Now you can build a new model with noisy structure and compare to the above, the code is nearly identical:
Note: in the model diagram there is now a tfq.layers.NoisyPQC
instead of a tfq.layers.PQC
since the depolarization probability is no longer zero. Training will take significantly longer since noisy simulation is far more expensive than noiseless.
Success: The noisy model still managed to train under some mild depolarization noise. Try experimenting with different noise models to see how and when training might fail. Also look out for noisy functionality under tfq.layers
and tfq.noise
.