qutip-qip icon indicating copy to clipboard operation
qutip-qip copied to clipboard

Simplify the structure of `CircuitSimulator`

Open BoxiLi opened this issue 1 year ago • 2 comments

Refactor the gate level CircuitSimulator

  • Avoid expanding all propagators and then applying them one by one. Only expand when applying the unitary. This saves memory and also make it easier for further improvement, i.e., exploring other ways to perform the matrix multiplication (e.g. einsum).
  • Switch some public members to private. Those members that track the execution of the circuit evaluation should not be public.
  • Small changes to improve the clarity.

BoxiLi avatar Dec 10 '23 18:12 BoxiLi

Remove the support of precompute_unitary This option computes the full unitary of all the gates and caches it. It is beneficial if the circuit has a small number of qubits but a very long depth. This feature makes the migration to the new efficient circuit evaluation very hard because the input gates are Qobjs and not gates. If the unitary for the circuit is desired, one can just call QubitCircuit.compute_unitary to get the circuit unitary and then apply it to different states.

Use np.einsum for gate-state multiplication Use np.einsum to apply the gate matrix directly on the state vector, instead of expanding it first to the full dimension.

  • The private member self._state will remain a numpy array during the sequential application of gates.
  • If a measurement occurs, the state has to be transformed to a Qobj.
  • Returning the state after each step is removed as it inevitably creates Qobj and creates a copy in v4.

Some benchmarking for the circuit simulator after the improvement, with qutip==4.7.3 or qutip==5.0.0a2, using qutip_qip.algorithems.qft: Note that the circuit simulator in the old version is very inefficient with v5 because qc.propagators() return a list of dense matrix! This needs to be deprecated (or fixed) in a different PR. image

BoxiLi avatar Dec 18 '23 09:12 BoxiLi

Could we also add tests (or maybe just parameterize some existing ones) for both the einsum and non-einsum modes?

@Simon there is no non-einsum mode anymore. There is no need to keep that option. Measurement is not yet compatible with einsum, so in that case it just fails back to normal Qobj.

BoxiLi avatar Apr 03 '24 07:04 BoxiLi