Skip to content

Instantly share code, notes, and snippets.

@friguzzi
Created September 19, 2020 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save friguzzi/3fb1006d2b81fd97d92858d7ddf5a284 to your computer and use it in GitHub Desktop.
Save friguzzi/3fb1006d2b81fd97d92858d7ddf5a284 to your computer and use it in GitHub Desktop.
using System;
using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Simulation.Simulators;
using System.Collections.Generic;
namespace Quantum.Search
{
class Driver
{
static void Main(string[] args)
{
using (var qsim = new QuantumSimulator())
{
IDictionary<string, int> dict = new Dictionary<string, int>();
for (int i = 0; i < 1000; i++)
{
IQArray<Result> res =Search.Run(qsim).Result;
string s = Convert.ToString(res);
int result;
if (dict.TryGetValue(s, out result))
{
dict.Remove(s);
dict.Add(s, result + 1);
}
else
{
dict.Add(s, 1);
}
System.Console.WriteLine($"Res:{s}");
}
foreach (KeyValuePair<string, int> item in dict)
{
Console.WriteLine("Key: {0}, Value: {1}", item.Key, item.Value);
}
}
}
}
}
namespace Quantum.Search {
open Microsoft.Quantum.Intrinsic;
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Math;
open Microsoft.Quantum.Oracles;
open Microsoft.Quantum.Arithmetic;
open Microsoft.Quantum.Characterization;
open Microsoft.Quantum.Arrays;
open Microsoft.Quantum.Measurement;
operation SprinklerAnc (queryRegister: Qubit[], target : Qubit) : Unit is Adj+Ctl
{
using (ancilla=Qubit[3 ])
{
X(queryRegister[2]);
X(ancilla[0]);
X(ancilla[1]);
X(ancilla[2]);
CCNOT(queryRegister[0],queryRegister[1],ancilla[0]);
CCNOT(queryRegister[1],queryRegister[2],ancilla[1]);
CCNOT(queryRegister[0],queryRegister[2],ancilla[2]);
(Controlled X)([ancilla[0],ancilla[1],ancilla[2],queryRegister[3]],target);
CCNOT(queryRegister[0],queryRegister[2],ancilla[2]);
CCNOT(queryRegister[1],queryRegister[2],ancilla[1]);
CCNOT(queryRegister[0],queryRegister[1],ancilla[0]);
X(ancilla[2]);
X(ancilla[1]);
X(ancilla[0]);
X(queryRegister[2]);
}
}
operation ApplyMarkingOracleAsPhaseOracle (markingOracle : ((Qubit[], Qubit) => Unit is Adj+Ctl), register : Qubit[] ) : Unit is Adj+Ctl
{
using (target = Qubit())
{
// Put the target into the |-⟩ state
X(target);
H(target);
// Apply the marking oracle; since the target is in the |-⟩ state,
// flipping the target if the register satisfies the oracle condition will apply a -1 factor to the state
markingOracle(register, target);
// Put the target back into |0⟩ so we can return it
H(target);
X(target);
}
}
// The Grover iteration
operation GroverIteration (register : Qubit[], oracle : ((Qubit[],Qubit) => Unit is Adj+Ctl)) : Unit is Ctl+Adj
{
ApplyMarkingOracleAsPhaseOracle(oracle,register);
ApplyToEachCA(H, register);
using (ancilla = Qubit()){
(ControlledOnInt(0, X))(register, ancilla); // Bit flips the ancilla to |1⟩ if register is |0...0⟩
Z(ancilla); // Ancilla phase (and therefore whole register phase) becomes -1 if above condition is satisfied
(ControlledOnInt(0, X))(register, ancilla); // Puts ancilla back in |0⟩
}
Ry(2.0 * PI(), register[0]);
ApplyToEachCA(H, register);
}
operation Search() : Result[]
{
using (reg=Qubit[4 ])
{
H(reg[0]);
H(reg[1]);
H(reg[2]);
H(reg[3]);
for (i in 1 ..2) {
GroverIteration(reg, SprinklerAnc);
}
// let query= Subarray([0,2],reg);
let state = MultiM(reg);
ResetAll(reg);
return state;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment