• Members 9 posts
    Nov. 1, 2021, 4:39 a.m.

    Is there any way that I can self define a control gate in qiskit? I have define a new quantum gate qg, and it will be applied to the "data" qubit if the "control" qubit is 1, and it will not be applied if the "control" qubit is 0. How can I implement this?

    Here is the definition of the quantum circuit

    n_control = 2
    n_data = 2
    control = QuantumRegister(n_control, 'control')
    data = QuantumRegister(n_data, 'data')
    qc = QuantumCircuit(control, data)
    

    The new quantum gate acts by

    qc.append(qg, data)
    

    and I want this operation to be controlled by control

  • Members 1 post
    Nov. 2, 2021, 3:28 a.m.

    Hi @Randomizer ,

    I don't know if you may have already solved this problem already but I think the closest tool Qiskit provides to do what you are asking is the ControlledGate Class.

    Taking the specifications (and prior code) you gave this was the closest working example I could come up with:

    from qiskit.circuit.library.standard_gates import XGate
    
    dual_qubit_control_gate = XGate().control(2)
    qc.append(dual_qubit_control_gate, [control[0], control[1], data[0]])
    

    With the first data qubit being the target qubit.

    I notice you have two qubits in your data register, so just to be thorough I've provided an example below with a two qubit controlled, two target qubit gate based:

    from qiskit.circuit.library.standard_gates import RZZGate
    import numpy as np
    
    dual_qubit_control_gate = RZZGate(theta=np.pi/2).control(2)
    qc.append(dual_qubit_control_gate, [control[0], control[1], data[0], data[1]])
    

    The code examples above all came from here: qiskit.org/documentation/stubs/qiskit.circuit.ControlledGate.html but I did my best to modify them to your existing setup.

    Hope that helps!