tequilahub / tequila

A High-Level Abstraction Framework for Quantum Algorithms
MIT License
370 stars 103 forks source link

Allow generic wavefunctions as initial_state #371

Closed ohuettenhofer closed 1 week ago

ohuettenhofer commented 1 week ago

This pull request depends on https://github.com/tequilahub/tequila/pull/370.

For the Qulacs and Qiskit backend, it makes it possible to initialize circuits with arbitrary wavefunctions instead of only basis states. This also adds the parameter for sampling and to tq.simulate.

I have added boolean flags to the backend circuit objects that indicate which backends support this and throw an error message when necessary. This doesn't seem ideal, but I didn't want to change the other backends that I can't test.

Also, combining initial states with a keymap is awkward, I think it only makes sense if we assume that all states on the inactive qubits are the same. For now I disabled it and throw an error message. I noticed that there's no keymap for sampling, can we get rid of it for simulating too?

Example usage:

import numpy as np
import tequila as tq
from tequila import QubitWaveFunction

U = tq.gates.H(target=0)
state = QubitWaveFunction.from_array(np.array([1.0, 1.0])).normalize()

result = tq.simulate(U, initial_state=state, backend="qulacs")
assert result.isclose(QubitWaveFunction.from_basis_state(n_qubits=1, basis_state=0))

result = tq.simulate(U, initial_state=state, backend="qulacs", samples=100)
assert result.isclose(QubitWaveFunction.from_array(np.array([100.0, 0.0])))
kottmanj commented 1 week ago

Should be fine (failed test is probabilistic and has nothing to do with the changes here). I Accidentally created some conflicts by squashing commits from the previous PR. Sorry :-)

ohuettenhofer commented 1 week ago

The conflict should be resolved, but I don't think there was a way to avoid this conflict. The alternative would be for me to keep the history clean directly so squashing isn't necessary, but then I have to rewrite the history every time I push a small change.

kottmanj commented 1 week ago

usually doesn't happen that much. You were just faster in development than I was with merging :-)