Giter Site home page Giter Site logo

Comments (2)

vadym-kl avatar vadym-kl commented on August 15, 2024

Using current Q# capabilities it is possible to express the example you refer to by using a combination of Assert and adjoint ( which seems like what uncompute should do):

namespace CCXwithQubitInZero
{
    open Microsoft.Quantum.Canon;
    open Microsoft.Quantum.Primitive;
    open Microsoft.Quantum.Extensions.Testing;

    /// # Summary
    /// Applies doubly controlled X to qubits assuming the target qubit is in zero state.
    /// For Adjoint version assumes that target qubit is returned back to zero
    /// after applying CCX.
    /// # Remarks 
    /// See Figure 3 in https://arxiv.org/pdf/1709.06648.pdf#page=3
    operation CCXwithTargetZero( control1 : Qubit, control2 : Qubit, target : Qubit ) : Unit {
        body(...) {
            AssertAllZero([target]);
            TStateFromZeroSt(target);
            CNOT(control1,target);
            CNOT(control2,target);

            CNOT(target,control1);
            CNOT(target,control2);
            
            Adjoint T(control1);
            Adjoint T(control2);
            T(target);

            CNOT(target,control1);
            CNOT(target,control2);

            H(target);
            S(target);            
        }
        adjoint(...) {
            AssertCCXTargetIsBackToZero(control1,control2,target);
            H(target);
            AssertProb([PauliZ],[target],One,0.5, "Probability of the measurement must be 0.5", 1e-10);
            if ( M(target) == One ) {
                Controlled Z([control1], control2); 
                X(target);
            }
        }
    }

    /// #Summary 
    /// Assert that target is going to be returned back to zero 
    /// after applying CCX
    operation AssertCCXTargetIsBackToZero( control1 : Qubit, control2 : Qubit, target : Qubit ) : Unit
    {
        With(
            ApplyToFirstThreeQubitsA(CCNOT,_),
            Assert([PauliI,PauliI,PauliZ],_,Zero,""),
            [control1,control2,target]);
    }

    /// # Summary 
    /// Uses CCXwithTargetZero to implement CCX with 4 T gates
    operation CCX( control1 : Qubit, control2 : Qubit, target : Qubit ) : Unit {
        body(...) {
            using( anc = Qubit() ) {
                WithA(CCXwithTargetZero(control1,control2,_),CNOT(_,target),anc);
            }
        }
        adjoint auto;
    }

    /// # Summary
    /// Creates |T⟩ state starting zero state
    operation TStateFromZeroSt ( target : Qubit ) : Unit {
        AssertAllZero([target]);
        H(target);
        T(target);
    }

    /// Test the correctness of CCX implementation
    operation CCXTest () : Unit {
        for( i in 0 .. 10 ) {
            AssertOperationsEqualReferenced(
                ApplyToFirstThreeQubitsA(CCX,_),
                ApplyToFirstThreeQubitsA(CCNOT,_),3);
        }        
        Message("Test passed");
    }
}

Note that AssertCCXTargetIsBackToZero must be replaced by NoOp for resource estimation purposes to get accurate gate counts.

from quantum.

Strilanc avatar Strilanc commented on August 15, 2024

I see. Yes, that is a good strategy for performing these kinds of operations-for-specific-states. Have a separate operation with an understood precondition (and postcondition for adjoint).

from quantum.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.