SimoneGasperini / qiskit-symb

Python package for the symbolic evaluation of parameterized quantum circuits in Qiskit
https://pypi.org/project/qiskit-symb/
Apache License 2.0
26 stars 2 forks source link

Added Qiskit standard gates and some tests #8

Closed sbrandhsn closed 7 months ago

sbrandhsn commented 8 months ago

Thanks for your repository, I saved me from a lot of hassle! :-) I added the remaining Qiskit standard gates and some tests, hope that is okay for you! I don't think I followed your standard approach of adding symbolic gates but so far it seems to work for my use case.

SimoneGasperini commented 7 months ago

Hi @sbrandhsn! Glad to hear that people like you found qiskit-symb useful! Thank you for the work you did adding more gates to the library. It's very useful for me especially to get a better understanding about the users needs and have a more precise idea about further improvements to the package.

In general, I think that implementing each single gate "by hand" one by one is something unsustainable in qiskit-symb, which instead is designed to cover all the commonly used unitaries "delegating" most of the work to Qiskit itself to be more robust and easier to maintain. If, for your specific use-case, you need to symbolically evaluate a quantum circuit that contains gates not currently supported in qiskit-symb, the most straightforward and convenient solution is to run a Qiskit transpilation pass including all the gates actually implemented in qiskit-symb to the basis_gates set. In most of the cases, something like this should do the job with a minimal overhead:

from qiskit import transpile
from qiskit_symb.utils import get_symbolic_gates_names

basis_gates = get_symbolic_gates_names()
transpiled_circuit = transpile(YOUR_CIRCUIT, basis_gates=basis_gates)

# qiskit_symb symbolic evaluation of the transpiled_circuit
# ...

Having said that, I also believe that even if the implementation you are proposing in this PR is working for your use-case, it would not be working for a general case: for instance, when the qubits a multi-qubit gate acts on are not adjacent within the considered quantum register. This is a quite delicate point and it requires a Pauli decomposition of the gate to be properly addressed. For further information, you can take a look to the open issue #2 concerning the XXMinusYYGate, XXPlusYYGate and DCXGate implementation, which will probably be one of the next additions to the library.

sbrandhsn commented 7 months ago

Hi @SimoneGasperini, thanks for your feedback! Yeah, using transpile makes sense in most cases. I forgot to add a small piece of code I adapted from the unitary simulator where the unitary of a quantum circuit is built from the gate unitaries by performing specific matrix multiplications. Thus, splitting up a gate unitary in its Pauli terms would not be necessary anymore in my understanding. At least it didn't appear to be necessary in my use case. :-)

SimoneGasperini commented 7 months ago

Hi again @sbrandhsn! I apologize for keeping you waiting but I did quite a bit of work to fit your contribution to the rest of the library. Indeed, the approach based on the Qiskit BasicAer unitary simulator you proposed turned out to work very well. The code is easier to read and qiskit-symb lower-level design looks now even closer to the original Qiskit library itself. Like you said, splitting up a gate unitary in its Pauli terms is not necessary anymore and this make adding new gates possible with a minimal effort. Thank you very much for the PR, if it's fine for you I would like to proceed with the merge.

PS: I removed some of the gates you implemented because I think their explicit implementation is not strictly necessary to support them in qiskit-symb (since they are internally built up starting from more basic gates when needed, for instance). Also, like already mentioned in one of the code review comments, I removed the U1Gate and U2Gate since they are deprecated in Qiskit (use PhaseGate and UGate instead).

sbrandhsn commented 7 months ago

Hi @SimoneGasperini, sure and thanks seeing this PR through! You are doing great work! :-)