1
$\begingroup$
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);
        }
    }

    adjoint auto;
}

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);
                }
            }
        }

        adjoint auto;
    }

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.

$\endgroup$
2
$\begingroup$

Yes, removing the ResetAll will prevent your code from running. Q# assumes that released qubits are in the 00..0 state.

One option is uncomputation - essentially, applying the adjoint of the operations you've applied. For instance,

Uncomputation example

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).

Q# has conjugation functionality that may be useful in your case!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.