hybridq icon indicating copy to clipboard operation
hybridq copied to clipboard

Better error messages when trying Kraus sampling

Open jsmarsha11 opened this issue 3 years ago • 0 comments

When we have a diagonal Kraus representation, such as sum_i s_i K_i rho K_i^dag, we should be able to sample, assuming that sum_i s_i K_i^dag K_i = Identity. If on the other hand there are some off-diagonal terms, e.g. K_i rho K_j, then we should not be able to sample.

In the latter case, we should throw an appropriate ValueError, but currently we get e.g. AttributeError: type object 'GlobalPauliChannel' has no attribute 'matrix'.

I propose that upon the user asking to sample, we:

  1. Check the Kraus operators are 'diagonal', and if not throw an appropriate error
  2. Check the Kraus operators form a valid quantum map (you can use is_channel in hybridq.noise.channel.utils). If that fails, it means it is not a valid quantum map, and we can not sample (since it can mean the probability distribution is not normalized). In this case we should throw another appropriate error.

If these pass then we can sample.

Some example code that can be tested

from hybridq.circuit.simulation import simulate
from hybridq.noise.channel import GlobalPauliChannel
from hybridq.dm.circuit import Circuit as SuperCircuit

gpc = GlobalPauliChannel([0], s=[0.5, 0.5, 0.1, 0])
c = SuperCircuit([gpc])

psi = simulate(c, '+', allow_sampling=True, remove_id_gates=False)

This should rightly fail (and it does currently), since Sum(s) != 1 (meaning it is not a valid probability distribution), but the error message is a little obtuse AttributeError: type object 'GlobalPauliChannel' has no attribute 'matrix'. This should fail check 2) above. Can also try making s a 2D array, and placing a non-zero off-diagonal, to check this fails case 1) above.

jsmarsha11 avatar Nov 12 '21 18:11 jsmarsha11