Commit f7ede468 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding python versions of cpp demos

parent 176c2804
Loading
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
// Create a general grover search algorithm.
// Let's create that marks 2 states
// Show figures Init - [Oracle - Amplification for i in iters] - Measure
// https://www.nature.com/articles/s41467-017-01904-7

// Show off kernel composition, common patterns, 
// functional programming (kernels taking other kernels)

// Show off kernel composition, common patterns, functional programming (kernels taking other kernels)
using GroverPhaseOracle = KernelSignature<qreg>;

__qpu__ void amplification(qreg q) {
+30 −0
Original line number Diff line number Diff line
from qcor import *

@qjit
def oracle_fn(q: qreg):
    CZ(q[0], q[2])
    CZ(q[1], q[2])
        
@qjit
def amplification(q: qreg):
    with compute:
        H(q)
        X(q)
    with action:
        Z.ctrl(q[0: q.size() - 1], q[q.size() - 1])
            
@qjit
def run_grover(q: qreg, oracle_var: KernelSignature(qreg), iterations: int):
    H(q)
    #Iteratively apply the oracle then reflect
    for i in range(iterations):
        oracle_var(q)
        amplification(q)
    # Measure all qubits
    Measure(q)

set_shots(1000)
q = qalloc(3)
run_grover(q, oracle_fn, 1)
counts = q.counts()
print(counts)
 No newline at end of file
+29 −0
Original line number Diff line number Diff line
from qcor import *

@qjit
def nothing(qbits : qreg, n : int, x : float):
  q = qbits.head()
  r = qbits.tail()

  for i in range(n):
    H(q)


  # This is Z Z = I
  Z(q)
  H(q)
  X(q)
  H(q)

  # Should be I if Rx on 0.0
  H(q)
  X.ctrl(q, r)
  Rx(q, x)
  X.ctrl(q, r)
  H(q)

set_opt_level(2)

qbits = qalloc(2)

print(nothing.n_instructions(qbits, 100, 0.0))