torchquantum icon indicating copy to clipboard operation
torchquantum copied to clipboard

[Feature Suggestion] `.control()` function

Open 01110011011101010110010001101111 opened this issue 1 year ago • 1 comments

Feature Suggestion: add a control() function to gates, similar to Qiskit's Gate.control option.

Creates a controlled unitary for an arbitrary unitary gate and a given control qubits

Args:
    num_ctrl_qubits (int): number of controlled qubits
    ctrl_state (str): controlled state expressed as a string of 1s and 0s (example: '110')

If an existing gate for the unitary and control state exists, we can return the gate. Otherwise, we can employ a similar technique to QubitUnitaryFast. Calling the function on an Operator can create a new Operator.

Usage:

crx = tq.RX(wires=1).control(1) # creates a crx

Currently thinking the best way to implement it would be adding an extra function to the operators.py Operations class. A work-in-progress idea right now; feel free to suggest any features or modifications in implementation!

Is this not the same functionality given by the func_controlled_unitary.py file here?

Although one issue I've run into with this is getting autograd to work after building controlled versions of my parameterized gates :(

teaguetomesh avatar Oct 25 '23 16:10 teaguetomesh