// Example 6-1: Applying the mirror subroutine to a flipped phase open Microsoft.Quantum.Arrays; open Microsoft.Quantum.Convert; open Microsoft.Quantum.Diagnostics; // Operation that flips the sign of the marked value in the register operation Flip (markedValue : Int, register : Qubit[]) : Unit is Adj { let bits = IntAsBoolArray(markedValue, Length(register)); ApplyPauliFromBitString(PauliX, false, bits, register); Controlled Z(Most(register), Tail(register)); ApplyPauliFromBitString(PauliX, false, bits, register); } // "Mirror" operation operation Mirror (register : Qubit[]) : Unit is Adj { within { ApplyToEachA(H, register); ApplyToEachA(X, register); } apply { Controlled Z(Most(register), Tail(register)); } } operation OneIteration () : Unit { let markedState = 3; // Allocate the qubit register use register = Qubit[4]; // Prep ApplyToEach(H, register); // Flip Flip(markedState, register); DumpMachine(); // Note that at this point the marked state will have negative amplitude // Mirror Mirror(register); DumpMachine(); // Note that after mirroring step the probability of measuring the marked state // (the first column in square brackets, also indicated by a row of asterisks before it) // is larger than the others // Make sure the qubits are back to the |0❭ state ResetAll(register); } %simulate OneIteration // Example 6-2: Repeated amplitude amplification iterations open Microsoft.Quantum.Arrays; open Microsoft.Quantum.Convert; open Microsoft.Quantum.Diagnostics; operation RepeatedIterations () : Unit { let markedState = 3; let numberOfIterations = 4; // Allocate the qubit register use register = Qubit[4]; // Prep ApplyToEach(H, register); DumpMachine(); for i in 1 .. numberOfIterations { Flip(markedState, register); Mirror(register); DumpMachine(); // Observe how the probability of measuring the marked state changes after each iteration } // Make sure the qubits are back to the |0❭ state ResetAll(register); } %simulate RepeatedIterations // Example 6-3: Amplitude amplification iterations with multiple values flipped open Microsoft.Quantum.Arrays; open Microsoft.Quantum.Convert; open Microsoft.Quantum.Diagnostics; // Operation that flips the sign of multiple marked values operation FlipMultipleStates (markedValues : Int[], register : Qubit[]) : Unit is Adj { ApplyToEachA(Flip(_, register), markedValues); } operation MultipleTerms () : Unit { let markedStates = [0, 1, 2]; let numberOfIterations = 4; // Allocate the qubit register use register = Qubit[4]; // Prep ApplyToEach(H, register); for (i in 1 .. numberOfIterations) { // Flip FlipMultipleStates(markedStates, register); // Mirror Mirror(register); DumpMachine(); } // Make sure the qubits are back to the |0❭ state ResetAll(register); } %simulate MultipleTerms