qutip / qutip-qip

The QuTiP quantum information processing package
https://qutip-qip.readthedocs.io/en/stable/
BSD 3-Clause "New" or "Revised" License
116 stars 63 forks source link

Regarding the types of the parameters of QubitCircuit.add_gate #179

Closed LeonWubben closed 1 year ago

LeonWubben commented 1 year ago

Currently the parameters for QubitCircuit.add_gate require:

  1. targets and controls to be a list
  2. arg_value to be a float

1. is fine, however just integers work as in the method it's straight up put in a Gate object which does accept both integers and lists. In one of your examples in Scheduler.schedule the targets and controls are also inputted as plain ints, which some IDEs will mark as errors or for example mypy just straight up gives it an error in CI/CD if you run a type checker. So my suggestion is to update the type hint in add_gate to also include integers inline with the Gate object.

My main issue is with 2. arg_value is an object that is put raw in a (potentially) user defined method for the gate. Two issues with that:

  1. It doesn't have to be a float. Might be some situations were you don't input an angle?
  2. It can be more than one argument

For example if you have a gate that implements a general single qubit you need two angles to define that: def r_gate(theta: float, phi: float) -> Qobj. However at most one parameter is allowed

So for now I have done something like qc.add_gate("r_gate", targets=[q], arg_values=(np.pi, 0)), (which gives a type error because (np.pi, 0) is a tuple, not a float). And then in the method I need to add theta, phi = args.
Whereas ideally I can just do qc.add_gate("r_gate", targets=q, theta=np.pi, phi=0) or qc.add_gate("r_gate", targets=q, args={"theta": np.pi, "phi": 0}. So I request to change the arg_value with a proper **args that are directly inputted into the gate function as arguments. Or at least change the type of arg_value to from float Any

BoxiLi commented 1 year ago

Hello @LeonWubben, sorry for the late reply. Thanks a lot for pointing those out!

Yes, you are right, the docs are a bit incomplete there. For the arg_value, indeed it can be a tuple or even a dictionary as long as it is adequately treated when creating the corresponding unitary. Something similar to qc.add_gate("r_gate", targets=q, args={"theta": np.pi, "phi": 0} is already used in a few places in qutip-qip.

You are very welcome to open a PR if you have time. Otherwise, I'll deal with it in a few days.