 # Gate adjoint not cancelling gate

I created a very specific circuit, and something weird is happening : if I add some doubly controlled H gates, and then their adjoint operation, I don’t get the initial state. I first thought it could be a bug, but as I’m not very experienced in quantum computing I wanted to check I didn’t do a mistake.

More concretely, here is a portion of code showing the problem:

``````dev = qml.device("default.qubit", wires=10, shots=5)

qml.ControlledQubitUnitary(matrix, control_wires=wires, wires=[wires])

qml.ControlledQubitUnitary(matrix, control_wires=wires, wires=[wires,wires])

@qml.qnode(dev)
def test():

return qml.state()

res = test()
for state in res:
if state.real < 0:
print(state)
``````

This shows 64 states with a negative real part. However, this should display 0 state, because the CCH gates and their adjoints are supposed to cancel each other.
And if I run the same code without the CCH part, I indeed get no negative state.

What’s going on, did I miss something or is it a bug ? (if that’s the case I can of course create a github issue)

Hi @Asimonu,

I think the main issue in your code is that you’re not adding Hadamards at the end. The best way of finding if the adjoint is working is by making sure you go back to the zero state. I have made a more simplified version of your code. I’ve tried to add a lot of comments to make it easier to understand but please let me know if you’re confused by any of the items here. Note that here I use less qubits and only one CCH plus its adjoint, but you can easily modify this to make it do exactly what you need.

``````import pennylane as qml

n_wires = 5 # Number of qubits
control1 = 0 # First control qubit
control2 = 1 # Second control qubit
target = 4 # Target qubit to apply the controlled operation to

dev = qml.device("default.qubit", wires=n_wires) # create the device

@qml.qnode(dev)
def small_test():
# apply the double controlled hadamard to the circuit
return qml.state()

fig, ax = qml.draw_mpl(small_test, expansion_strategy='device', show_all_wires='true')()
fig.show()
``````

If you need to make the controlled version of more complex gates it may be worth creating a function for these sets of gates. You can learn more about `qml.ctrl` here.

For the drawing you can remove the `expansion_strategy='device'` in case you want to show the block instead of the decomposition.

Please let me know if this helps!

1 Like

Thanks for the good practice tips, in particular for the good way to do a multi controlled gate.
The problem with the negative real part still occurs, but I realized it is always equal to something very small, so it is likely that float imprecision is responsible of it.

Hi @Asimonu,

``````res = small_test()