Qiskit / qiskit

Qiskit is an open-source SDK for working with quantum computers at the level of extended quantum circuits, operators, and primitives.
https://www.ibm.com/quantum/qiskit
Apache License 2.0
4.89k stars 2.3k forks source link

add ctrl_state to MCMT gate #10698

Open Marsmmz opened 11 months ago

Marsmmz commented 11 months ago

What should we add?

Add ctrl_state to multi-control multi-target gates

diemilio commented 11 months ago

I think this would be a nice feature to add. Willing to help implement it if looking for help.

Dpbm commented 10 months ago

This is awesome, for any help, I'm here too!

ShashiQubit commented 9 months ago

Hi! I am interested in working on this issue. Can this be assigned to me? .I would like to make my first contribution to qiskit terra.

Dpbm commented 9 months ago

Yeah man, if @diemilio and @Marsmmz also agree, you can do this! And if during your task you find some issues, please tell us and we may help you😁

diemilio commented 9 months ago

Sure! I had volunteered to take care of this a little while back cause I had the time, but I never got it assigned.

ShashiQubit commented 9 months ago

@diemilio Thanks for the opportunity. I have started working on the issue following the contribution guide.

Marsmmz commented 8 months ago

Sorry for getting here late, I'll try my best to help!

ShashiQubit commented 8 months ago

@Marsmmz I have a question. Does MCMT controls need to be custom state?. Meaning should user given freedom to specify whether the state of control is 0 or 1 or mix like '01...' control state targeting q_k number of qubits. If yes what's best way to take that input from user, dictionary or list??

Marsmmz commented 8 months ago

@Marsmmz I have a question. Does MCMT controls need to be custom state?. Meaning should user given freedom to specify whether the state of control is 0 or 1 or mix like '01...' control state targeting q_k number of qubits. If yes what's best way to take that input from user, dictionary or list??

I personally think users should have the freedom to specify the control state. Like if a 2-C-2-T gate is needed, users can specify which of '00', '01', '10', '11' they want to be the control state. Perhaps taking a 01 binary string as input is enough? What do you think?@ShashiQubit @diemilio @Dpbm

ShashiQubit commented 7 months ago

@Marsmmz @diemilio @Dpbm I want feed back on this modified implementation of control function from source code of MCMT gate:

def control(self, num_ctrl_qubits=1, label=None, ctrl_state=None):
        """Return the controlled version of the MCMT circuit."""
        mcmt_circuit = QuantumCircuit(self.num_ctrl_qubits + num_target_qubits)
        if isinstance(ctrl_state, str):
            if ctrl_state == '0':  # If '0', apply X gates to all control qubits (0-controlled)
                for i in range(self.num_ctrl_qubits):
                    mcmt_circuit.x(i)
                mcmt_circuit.append(MCMT(self.gate, self.num_ctrl_qubits + num_ctrl_qubits, self.num_target_qubits), mcmt_circuit.qubits)
                for i in range(self.num_ctrl_qubits):
                    mcmt_circuit.x(i)
            elif ctrl_state == '1':  # If '1', do nothing special to MCMT (1-controlled)
                mcmt_circuit.append(MCMT(self.gate, self.num_ctrl_qubits + num_ctrl_qubits, self.num_target_qubits), mcmt_circuit.qubits)
            else:  # If string of '0's and '1's, handle accordingly
                for i in range(len(ctrl_state)):
                    if ctrl_state[i] == '0':
                        mcmt_circuit.x(i)
                mcmt_circuit.append(MCMT(self.gate, self.num_ctrl_qubits + num_ctrl_qubits, self.num_target_qubits), mcmt_circuit.qubits)
                for i in range(len(ctrl_state)):
                    if ctrl_state[i] == '0':
                        mcmt_circuit.x(i)

        return mcmt_circuit

In this version, if the ctrl_state is '0', X gates are applied to all the control qubits both before and after the MCMT gate, so we have a "0-controlled" MCMT. If ctrl_state is '1', the MCMT gate is implemented directly without any extra X gates, meaning it's a regular "1-controlled" MCMT. If ctrl_state is a string containing '0's and '1's, X gates are applied before and after the MCMT gate to the qubits corresponding to '0's in ctrl_state.

Also what additional test cases are needed apart from the default ones already present for MCMT ? One that I could think is to check length of ctrl_state string matches the length of control qubits if ctrl_state is not None.