Grover's algorithm described in Section 2, Algorithm 1.
In this example, Oracle is expressed just with Toffoli (CCNOT) gate, and this is not a general implementation, as far as I understand.
In the literature, they constructed CCNOT with natively supported gates, but Q# has it, and I just used it (control and ancillary are a bit confusing to me though).
By running my sample code the numbers of measurements observed in 1000 trials are;
00:0, 01:0, 10:0, 11:1000
Unlike the previous one, the result is completely the same as the theory.
I feel I should split Q# and C# parts in a better way.
In any case, I am getting used to it, hopefully.
Sample codes are;
Q# (Program.qs)
/// # Summary
///
namespace Grover {
open Microsoft.Quantum.Canon;
open Microsoft.Quantum.Intrinsic;
operation Grover() : (Int,Int,Int,Int) {
Message("This is Q# implementation of Algorithm 1 in Quantum Algorithm Implementaions for Beginners");
mutable n00 = 0;
mutable n01 = 0;
mutable n10 = 0;
mutable n11 = 0;
for(trial in 1..1000){
using ( (qubit0, qubit1, qubit2) = (Qubit(), Qubit(),Qubit()) ){
// qubit0: ancillar
// qubit1, 2: x1, x2
//Initializaiton
X(qubit0);
H(qubit0);
H(qubit1);
H(qubit2);
// qubit0 is called ancillar, looking at the following site and the article, it should be the target
// https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
CCNOT(qubit1,qubit2,qubit0); //Toffoli gate
// Grover diffusion operator
H(qubit1);
X(qubit1);
H(qubit1);
H(qubit2);
X(qubit2);
CNOT(qubit2,qubit1);
H(qubit1);
X(qubit1);
H(qubit1);
X(qubit2);
H(qubit2);
let res0 = M(qubit1);
let res1 = M(qubit2);
if(res0 == Zero){
if(res1 == Zero){
set n00 += 1;
}else{
set n01 += 1;
}
}else{
if(res1 == Zero){
set n10 += 1;
}else{
set n11 += 1;
}
}
Reset(qubit0);
Reset(qubit1);
Reset(qubit2);
}
}
return(n00,n01,n10,n11);
}
}
C# (Driver.cs)
using System;
using Microsoft.Quantum.Simulation.Core;
using Microsoft.Quantum.Simulation.Simulators;
namespace Grover
{
class Driver
{
static void Main(string[] args)
{
using (var qsim = new QuantumSimulator())
{
long a,b,c,d;
(a,b,c,d) = Grover.Run(qsim).Result;
Console.WriteLine("{0},{1},{2},{3}",a,b,c,d);
}
}
}
}
0 件のコメント:
コメントを投稿