Skip to content

Commit

Permalink
modify core
Browse files Browse the repository at this point in the history
  • Loading branch information
guochu committed Nov 9, 2020
1 parent d142e90 commit 6a69f72
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 4 deletions.
2 changes: 1 addition & 1 deletion randomquantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def generate_circuits(self, depth):
# height
n = 8
# circuit depth
depth = 16
depth = 10


maxbonddimension = 100
Expand Down
Binary file modified rqc/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file modified rqc/__pycache__/gates.cpython-37.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions rqc/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

from .circuit2d import *
from .overlap import *
from .measure import *
Binary file modified rqc/core/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file modified rqc/core/__pycache__/circuit2d.cpython-37.pyc
Binary file not shown.
Binary file added rqc/core/__pycache__/measure.cpython-37.pyc
Binary file not shown.
Binary file modified rqc/core/__pycache__/overlap.cpython-37.pyc
Binary file not shown.
Binary file modified rqc/core/__pycache__/peps.cpython-37.pyc
Binary file not shown.
75 changes: 75 additions & 0 deletions rqc/core/measure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
# @Author: guochu
# @Date: 2020-11-09 10:55:24
# @Last Modified by: guochu
# @Last Modified time: 2020-11-09 11:23:08
from numpy.random import uniform
from numpy import sqrt
from rqc.tensor import astensor
from .circuit2d import OneBodyGate

__all__ = ['OneBodyObserver']

def _discrete_sample(l):
s = sum(l)
# print('sum is', s)
if (abs(s) < 1.0e-12):
# print('s is', s)
raise ValueError('error in measure.')
l1 = [None]*(len(l)+1)
l1[0] = 0.
for i in range(len(l)):
l1[i+1] = l1[i] + l[i]/s
s = uniform(low=0., high=1.)
for i in range(len(l1)-1):
if (s >= l1[i] and s < l1[i+1]):
return i
raise NotImplementedError('should not be here.')


class OneBodyObserver:
"""docstring for OneBodyObserver"""
def __init__(self, key):
if len(key) != 2:
raise ValueError('wrong position for one body gate.')
for s in key:
if not isinstance(s, int):
raise TypeError('position must be integer type.')
self.key = tuple(key)

def apply(self, state, maxbonddimension=2000, svdcutoff=1.0e-8, verbose=1):
up = astensor([[1., 0.], [0., 0.]])
down = astensor([[0., 0.], [0., 1.]])
up_gate = OneBodyGate(self.key, up)
# ms = close_peps(state, state, {self.key:up})
state_1 = state.copy()
up_gate.apply(state, maxbonddimension=maxbonddimension, svdcutoff=svdcutoff, verbose=verbose)
s = state_1.cross(state, conj=True, scale_factor=1.)
tol = 1.0e-6
if s.imag > tol:
warnings.warn('the imaginary part of result is larger than'+str(tol))
s = abs(s.real)
l = [s, abs(1-s)]
i = _discrete_sample(l)
# print('probability is', s, 'istate is', i)
if i==0:
state[self.key] = up.contract(state[self.key], ((1,), (0,)))
state[self.key] /= sqrt(l[i])
else:
state[self.key] = down.contract(state[self.key], ((1,), (0,)))
state[self.key] /= sqrt(l[i])
self.istate = i
self.probability = l[i]

def apply_and_collect(self, state, result, maxbonddimension=2000, svdcutoff=1.0e-8, verbose=1):
self.apply(state, maxbonddimension, svdcutoff, verbose=verbose)
result.append(self.result)


@property
def name(self):
return 'Q:Z' + str(self.key)

@property
def result(self):
return (self.name, self.istate)
7 changes: 4 additions & 3 deletions rqc/core/peps.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ def __setitem__(self, key, value):
self.data[self.__sgi(key)] = astensor(value)

def copy(self):
r = type(self)(self.m, self.n)
for i in range(self.m):
for j in range(self.n):
m, n = self.shape
r = type(self)(self.shape)
for i in range(m):
for j in range(n):
r[(i, j)] = self[(i, j)].copy()
return r

Expand Down
Binary file modified rqc/tensor/__pycache__/__init__.cpython-37.pyc
Binary file not shown.
Binary file modified rqc/tensor/__pycache__/tensor.cpython-37.pyc
Binary file not shown.
Binary file modified rqc/tensor/__pycache__/util.cpython-37.pyc
Binary file not shown.

0 comments on commit 6a69f72

Please sign in to comment.