MultiControlledX not working

Hi there!

I’m working on a circuit in PennyLane with qml.MultiControlledX and I’m unable to get it to work without error. Maybe there’s a bug here, but I’m confused, so not 100% confident.

Example code:

dev = qml.device("default.qubit", wires=3)
@qml.qnode(dev)
def circuit(x):
    qml.Hadamard(0)
    qml.Hadamard(1)
    qml.RX(x, 0)
    qml.MultiControlledX([0,1], [0,1,2], [1,1])
    return qml.expval(qml.PauliZ(1) @ qml.PauliZ(2))

circuit(0.5)

When I run it, it says “ValueError: MultiControlledX accepts a single target wire.”

I also tried calling with the argument names (qml.MultiControlledX(control_wires=[0,1], wires=[0,1,2], control_values=[1,1])) and it also didn’t work.

Any help would be very nice :slight_smile:

Hi @therealsqueezingpand !

Thank you for raising this point! Is a very particular situation and may help others :smile: .

Implementing qml.MultiControlledX works a bit differently. You can find more details in this link if you’d like to explore further. Essentially, the control_wires parameter is not necessary. In the wires parameter, the last value corresponds to the target qubit, while the preceding values represent the control qubits.

dev = qml.device("default.qubit", wires=3)
@qml.qnode(dev)
def circuit(x):
    qml.Hadamard(0)
    qml.Hadamard(1)
    qml.RX(x, 0)
    qml.MultiControlledX(None,[0,1,2],[1,1])
    return qml.expval(qml.PauliZ(1) @ qml.PauliZ(2))

circuit(0.5)

You can avoid the parameter with the value None, or you can use this other way:

dev = qml.device("default.qubit", wires=3)
@qml.qnode(dev)
def circuit(x):
    qml.Hadamard(0)
    qml.Hadamard(1)
    qml.RX(x, 0)
    qml.MultiControlledX(wires=[0,1,2], control_values=[1,1])
    return qml.expval(qml.PauliZ(1) @ qml.PauliZ(2))

circuit(0.5)

in both case we avoid the parameter control_wires, and the result if is correct is the following image

image

I hope my answer was helpful :smiley:

In case it wasn’t clearer, or you need more information, let me know.

@maldoalberto Thanks for your answer! Your suggested code worked perfectly. I don’t know why I didn’t spot that before :person_facepalming:

1 Like

@therealsqueezingpand, I’m glad it has helped you, as a recommendation is important read the documentation of the methods and if there is not something clear for that is the forum, or if you think you do not understand the text you can help to contribute to improve it with a PR :smiley: