operation Task12 (queryRegister : Qubit[], target : Qubit, pattern : Int[]) : Unit {

body (...) {
using (c = Qubit[Length(queryRegister)]) {

ApplyToEachA(X, c);

for (i in 0..Length(queryRegister) - 1) {

if (pattern[i] == 0) {
CNOT(queryRegister[i], c[i]);
} elif (pattern[i] == 1)  {
X(c[i]);
CNOT(queryRegister[i], c[i]);
}
}

Controlled X(c, target);
// ResetAll(c);
}
}

}


I want to uncomment that ResetAll(c) line as I believe the right thing to do is to return the system the qubits in the same state as they were borrowed. But somehow the adjoint auto gives an error to the ResetAll(c) line. Will my code work just fine without the Reset line?

Edit:

operation Task12 (queryRegister : Qubit[], target : Qubit, pattern : Int[]) : Unit {

body (...) {
using (c = Qubit[Length(queryRegister)]) {

within {

ApplyToEachA(X, c);

for (i in 0..Length(queryRegister) - 1) {

if (pattern[i] == 0) {
CNOT(queryRegister[i], c[i]);
} elif (pattern[i] == 1)  {
X(c[i]);
CNOT(queryRegister[i], c[i]);
}
}
}

apply {

Controlled X(c, target);
}
}
}

}


Will this work? I don't have the driver file of this code, hence have to rely completely off the error detection of vscode. Thanks for help.

The first two ancilla qubits are left unchanged by the last operation, but the last qubit has been modified and is dependent on $$x_1, x_2, x_3$$. In your case, you'd want to apply the adjoints of your operations in reverse order ($$U_a U_b CX U_b^{\dagger} U_a^{\dagger}$$, where $$U_a U_b$$ are arbitrary unitaries and $$CX$$ is the CNOT gate).