Access quantum state at any point of the circuit


#1

Is there any way to print out the explicit state of the device at any point in the circuit?


#2

Hi @AdityaDendukuri!

Since PennyLane is designed to work with real hardware, this is not possible — we can only extract data we could expect to obtain in the lab, in this case measurement statistics such as expectation values, single shot samples, and variances.

However, if you are using a PennyLane simulated device, such as default.qubit, strawberryfields.fock, strawberryfields.gaussian, etc., and it has been defined as

dev = qml.device("device.name", ...)

there are ways to obtain the wavefunction after a QNode has been executed:

  • default.qubit and default.gaussian: the wavefunction is accessible in dev._state

  • strawberryfields.fock and strawberryfields.gaussian: the engine is accessible in dev.eng, and the state accessible in dev.state

  • forest.wavefunction: the state is accessible in dev.state

Since this is not a documented approach, every plugin is free to do this differently, and some plugin devices don’t even have wavefunctions to access, such as forest.qvm. Automatic differentiation of these device wavefunctions is also not supported.

Hope this helps!