【コンテストブログ】量子断熱計算をやってみた【ソースコード】

【コンテストブログ】量子断熱計算をやってみた by Hikaru Wakaura | blueqat」で利用したソースコードです。よろしければ自分の環境で試してみて頂けると幸いです。


ソースコードを研究、商業目的で利用したい場合はご一報ください。


If you use this source for research or commercial purpose, please inform me.


hikaruwakaura@gmail.com

from blueqat import Circuit, pauli, vqe
from blueqat.pauli import qubo_bit as q
from math import pi
import numpy as np
import csv


from collections import Counter, defaultdict
from functools import reduce
import itertools




def Auu(i):
    return 0.5*(pauli.Z[i]*pauli.Z[i]+pauli.Z[i])

def Add(i):
    return 0.5*(pauli.Z[i]*pauli.Z[i]-pauli.Z[i])

def Aud(i):
    return 0.5*(pauli.X[i]+1.j*pauli.Y[i])    

def Adu(i):
    return 0.5*(pauli.X[i]-1.j*pauli.Y[i])    


def GroverAQCi(s, N):
    He = pauli.Z[0]*pauli.Z[0]
    psi = pauli.X[0]
    for j in range(1, N):
        psi = psi*pauli.X[j]
        He = pauli.Z[j]*pauli.Z[j]*He
    Hi = He-psi*(1-s)/(2**N)
    return Hi


def GroverAQCd(s, N, ans):
    
    psi = pauli.X[0]
    for j in range(1, N):
        psi = psi*pauli.X[j]
    od, do = pauli.I, pauli.I
    ov = pauli.I
    binstate = format(ans, '04b')
    for j in range(len(binstate) - 1, -1, -1):
        print(j, binstate[N - 1 - j])
        if(binstate[N - 1 - j] == '1'):
            od = od*(Adu(j)+Add(j))
            do = do*(Aud(j)+Add(j))
            ov = ov*Add(j)
        else:
            od = od*(Auu(j)+Aud(j))
            do = do*(Auu(j)+Adu(j))
            ov = ov*Auu(j)
    Hi = psi - od - do + ov
    return - Hi*s/(2**N-1)


def AQCGrovQA(ep, s, ndT, nq, cp, ans):
    t = 1/(2*ep)*(np.arctan(np.sqrt(2**nq-1)*(2*s-1))+np.arctan(np.sqrt(2**nq-1)))*(2**nq/(2**nq-1))
    c = cp.copy()
    Hi = GroverAQCi(t/ndT, nq)
    Hd = GroverAQCd(t/nndT, nq, ans)
    Htot = (Hi+Hd).to_expr().simplify()
    evos = [term.get_time_evolution() for term in Htot.terms]
    for evo in evos:
        evo(c, 1/ndT*np.pi)


    sampler = non_sampling_sampler
    ene = get_expectation(c, sampler, Htot)
    return c, ene


def expect(qubits, meas):
    "For the VQE simulation without sampling."
    result = {}
    i = np.arange(len(qubits))
    meas = tuple(meas)


    def to_mask(n):
        return reduce(lambda acc, im: acc | (n & (1 << im[0])) << (im[1] - im[0]), enumerate(meas), 0)


    def to_key(k):
        return tuple(1 if k & (1 << i) else 0 for i in meas)


    mask = reduce(lambda acc, v: acc | (1 << v), meas, 0)


    cnt = defaultdict(float)
    for i, v in enumerate(qubits):
        p = v.real ** 2 + v.imag ** 2
        if p != 0.0:
            cnt[i & mask] += p
    return {to_key(k): v for k, v in cnt.items()}


def non_sampling_sampler(circuit, meas):
    """Calculate the expectations without sampling."""
    meas = tuple(meas)
    n_qubits = circuit.n_qubits
    return expect(circuit.run(returns="statevector"), meas)


def get_expectation(circuit, sampler, observable):
    """Calculate energy from circuit and sampler."""
    val = 0.0
    for meas in observable:
        c = circuit.copy()
        for op in meas.ops:
            if op.op == "X":
                c.h[op.n]
            elif op.op == "Y":
                c.rx(-np.pi / 2)[op.n]
        measured = sampler(c, meas.n_iter())
        for bits, prob in measured.items():
            if np.sum(bits) % 2:
                val -= prob * meas.coeff
            else:
                val += prob * meas.coeff
    return val.real

ndT, nq, ans = 100, 4, 2
ep = 0.001
N = 2**nq
c = Circuit(nq)
c.h[:]
for t in range(ndT):
    c, ene = AQCGrovQA(ep, t/ndT, ndT, nq, c, ans)
    ct = c.copy()
    answer = ct.run(shot=100)
    anst =  [np.linalg.norm(answer[j]) for j in range(2**nq)]
    theo = [t/ndT]
    for j in range(len(anst)):
        theo.append(anst[j])
    with open('AQCGrovtheo.csv', 'a') as en:
        writer = csv.writer(en)
        writer.writerow(theo)
    with open('AQCGrovtheoe.csv', 'a') as en:
        writer = csv.writer(en)
        writer.writerow([t/ndT, ene])
        
    print(anst)
Hikaru Wakaura
個人研究者の若浦 光です。量子アルゴリズムの実装結果や論文の紹介などを載せていきます。 mail: hikaruwakaura@gmail.com
Comments
Hikaru Wakaura
個人研究者の若浦 光です。量子アルゴリズムの実装結果や論文の紹介などを載せていきます。 mail: hikaruwakaura@gmail.com
Related posts

blueqat Inc.

Shibuya Scramble Square 39F 2-24-12, Shibuya, Shibuya-ku, Tokyo
Contact: info@blueqat.com