Skip to content
Permalink
Browse files

feature-hokusai release candidate ver.0.0.2 (#17)

* added many features.
 - Using SymPy, bypass to quantum computing executors/simulators.
 - Compatibility test with sympy.
 - Ising simulator.
  • Loading branch information...
openql-org committed Jan 24, 2018
1 parent 523c8c1 commit c2f0ec0adb75b07b7ddebbf356b44ffbb4af1bf7
Showing with 10,366 additions and 33 deletions.
  1. +17 −0 .travis.yml
  2. +9 −0 AUTHORS
  3. +15 −0 CONTRIBUTING.rst
  4. +6 −0 HISTORY.rst
  5. +22 −1 README.rst
  6. +2 −0 quantpy/__init__.py
  7. +8 −0 quantpy/conftest.py
  8. +33 −0 quantpy/ising/qubo.py
  9. +53 −0 quantpy/ising/sqa.py
  10. +17 −0 quantpy/ising/tests/test_qubo.py
  11. +15 −0 quantpy/ising/tests/test_sqa.py
  12. +3 −1 quantpy/release.py
  13. +22 −0 quantpy/sympy/__init__.py
  14. +1 −0 quantpy/sympy/executor/__init__.py
  15. +63 −0 quantpy/sympy/executor/_base_quantum_executor.py
  16. +111 −0 quantpy/sympy/executor/classical_simulation_executor.py
  17. +37 −0 quantpy/sympy/executor/ibmq_executor.py
  18. +1 −0 quantpy/sympy/executor/simulator/__init__.py
  19. +30 −0 quantpy/sympy/executor/simulator/_base_simulator.py
  20. +178 −0 quantpy/sympy/executor/simulator/numpy_simulator.py
  21. +18 −0 quantpy/sympy/executor/sympy_executor.py
  22. +14 −0 quantpy/sympy/expr_extension.py
  23. +27 −0 quantpy/sympy/qapply.py
  24. +63 −0 quantpy/sympy/tests/sympy/LICENSE
  25. 0 quantpy/sympy/tests/sympy/physics/quantum/__init__.py
  26. +56 −0 quantpy/sympy/tests/sympy/physics/quantum/test_anticommutator.py
  27. +46 −0 quantpy/sympy/tests/sympy/physics/quantum/test_boson.py
  28. +100 −0 quantpy/sympy/tests/sympy/physics/quantum/test_cartesian.py
  29. +178 −0 quantpy/sympy/tests/sympy/physics/quantum/test_cg.py
  30. +71 −0 quantpy/sympy/tests/sympy/physics/quantum/test_circuitplot.py
  31. +401 −0 quantpy/sympy/tests/sympy/physics/quantum/test_circuitutils.py
  32. +67 −0 quantpy/sympy/tests/sympy/physics/quantum/test_commutator.py
  33. +14 −0 quantpy/sympy/tests/sympy/physics/quantum/test_constants.py
  34. +69 −0 quantpy/sympy/tests/sympy/physics/quantum/test_dagger.py
  35. +288 −0 quantpy/sympy/tests/sympy/physics/quantum/test_density.py
  36. +37 −0 quantpy/sympy/tests/sympy/physics/quantum/test_fermion.py
  37. +344 −0 quantpy/sympy/tests/sympy/physics/quantum/test_gate.py
  38. +92 −0 quantpy/sympy/tests/sympy/physics/quantum/test_grover.py
  39. +107 −0 quantpy/sympy/tests/sympy/physics/quantum/test_hilbert.py
  40. +493 −0 quantpy/sympy/tests/sympy/physics/quantum/test_identitysearch.py
  41. +72 −0 quantpy/sympy/tests/sympy/physics/quantum/test_innerproduct.py
  42. +138 −0 quantpy/sympy/tests/sympy/physics/quantum/test_matrixutils.py
  43. +229 −0 quantpy/sympy/tests/sympy/physics/quantum/test_operator.py
  44. +43 −0 quantpy/sympy/tests/sympy/physics/quantum/test_operatorordering.py
  45. +71 −0 quantpy/sympy/tests/sympy/physics/quantum/test_operatorset.py
  46. +125 −0 quantpy/sympy/tests/sympy/physics/quantum/test_pauli.py
  47. +26 −0 quantpy/sympy/tests/sympy/physics/quantum/test_piab.py
  48. +893 −0 quantpy/sympy/tests/sympy/physics/quantum/test_printing.py
  49. +120 −0 quantpy/sympy/tests/sympy/physics/quantum/test_qapply.py
  50. +96 −0 quantpy/sympy/tests/sympy/physics/quantum/test_qasm.py
  51. +53 −0 quantpy/sympy/tests/sympy/physics/quantum/test_qexpr.py
  52. +48 −0 quantpy/sympy/tests/sympy/physics/quantum/test_qft.py
  53. +232 −0 quantpy/sympy/tests/sympy/physics/quantum/test_qubit.py
  54. +190 −0 quantpy/sympy/tests/sympy/physics/quantum/test_represent.py
  55. +27 −0 quantpy/sympy/tests/sympy/physics/quantum/test_rshift_as_append_circuit.py
  56. +122 −0 quantpy/sympy/tests/sympy/physics/quantum/test_sho1d.py
  57. +22 −0 quantpy/sympy/tests/sympy/physics/quantum/test_shor.py
  58. +4,290 −0 quantpy/sympy/tests/sympy/physics/quantum/test_spin.py
  59. +230 −0 quantpy/sympy/tests/sympy/physics/quantum/test_state.py
  60. +109 −0 quantpy/sympy/tests/sympy/physics/quantum/test_tensorproduct.py
  61. +36 −0 quantpy/sympy/tests/test_hadamard.py
  62. +38 −0 quantpy/sympy/tests/test_hadamard_numpy.py
  63. +3 −0 requirements.txt
  64. +25 −31 setup.py
@@ -0,0 +1,17 @@
language: python
sudo: false
python:
- "3.5"
- "3.6"
# command to install dependencies
install:
- pip install -r requirements.txt
before_script: # configure a headless display to test plot generation
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
- sleep 3 # give xvfb some time to start
# command to run tests
script:
- pytest # or py.test for Python versions 3.5 and below
notifications:
email: false
@@ -0,0 +1,9 @@
All people who contributed to QuantPy by sending at least a patch or more (
in the order of the date of their first contribution), except those who
explicitly didn't want to be mentioned.

Kodack <kodack64@gmail.com>
Takumi Kato <takumi.kt+github@gmail.com>
Shumpei Kobayashi <skonb@w-ax.is>
Yuichiro Minato(MDR) <minato@mdrft.com>
Kiyohito Yamazaki <kyamaz@openql.org>
@@ -0,0 +1,15 @@
## Contributing

First, thank you!

Contributions to the OpenQL libraries are very welcome! We can't do this alone.
You can contribute in different ways:
spread the word, report bugs, come up with ideas and suggestions, and contribute to the code.

There are a few preferences regarding code contributions:

- OpenQL's python source codes follow the Style Guide for Python Code as described [here](http://legacy.python.org/dev/peps/pep-0008/)
- Make sure you properly unit test your changes.
- Run the unit tests to make sure they all pass.
- Write commit messages that make sense, and rebase your branch before submitting your pull request.
- Send pull requests to the `feature-{%code-name%}` branch, not the `master` branch and the `develop` branch.
@@ -0,0 +1,6 @@
# History

## 2017-09-28, version 0.0.1

First publish of the quantpy package. (package is still empty)

@@ -1 +1,22 @@
# quantpy
quantpy
=========
QuantPy is usefull libraries and emulator using Quantum Computer system in Python.


Installation
------------
QuantPy has a hard dependency NumPy, SymPy, QISKit. You should install them first.

To install QunatPy itself, then simply run::

$ pip install qunatpy


Bugs
----
Our issue tracker is at https://github.com/openql-org/quantpy/issues. Please report any bugs that you find. Or, even better, fork the repository on GitHub and create a pull request. We welcome all changes, big or small, and we will help you make the pull request if you are new to git.


License
-------
QunatPy is BSD licensed, so you are free to use it whatever you like, be it academic, commercial, creating forks or derivatives, as long as you copy the BSD statement if you redistribute it (see the LICENSE file for details).
@@ -1,3 +1,5 @@
# -*- coding:utf-8 -*-

def main():
"""Entry point for the application script"""
print("")
@@ -0,0 +1,8 @@
# -*- coding:utf-8 -*-

from __future__ import print_function, division
import sys
sys._running_pytest = True
from distutils.version import LooseVersion as V

import pytest
@@ -0,0 +1,33 @@
# -*- coding:utf-8 -*-
"""Utilities of QUBO matrix
"""

import numpy as np

def getMat(qubo):
"""Return array([h,J,Jsum+hsum])
"""
N = len(qubo)
h = []
J = [[0]*N for i in range(N)]
Jsum = 0
hsum = 0

for j in range(N):
sum = 0

for i in range(j+1,N):
r = qubo[j][i]
sum += r
J[j][i] = r*1.0/4
J[i][j] = r*1.0/4
Jsum += r*1.0/4

for i in range(0,j):
sum += qubo[j][i]

s = qubo[j][j]
hsum += s*1.0/2
h.append(s*1.0/2 + sum)

return np.array([h,J,Jsum+hsum])
@@ -0,0 +1,53 @@
# -*- coding:utf-8 -*-
"""Utilities of Simulated [Quantum] Annealing.
"""
from numpy import *

#run sqa algorithm with params
def run(kT,Ginit,m,mat,rep):
"""run simulated QA.
@param kT
@param Ginit
@param m
@param mat
@param rep
@return array([qarr,Earr])
"""
h = mat[0]
J = mat[1]
c = mat[2]
N = len(h)
tau = 0.99
Gfin = 0.01
qarr = []
Earr = []

# simulated quantum annealing simulator using quantum monte carlo & metropolis

for j in range(rep):
G = Ginit

q = []
for i in range(m):
q.append(random.choice([-1,1],N))

while G > Gfin:
for i in range(N*m):
x = random.randint(N)
y = random.randint(m)
dE = (2*q[y][x]*(h[x]+q[y][(N+x-1)%N]*J[x][(N+x-1)%N]+q[y][(x+1)%N]*J[x][(x+1)%N]))*1.0/m
dE += -q[y][x]*(q[(m+y-1)%m][x]+q[(y+1)%m][x])*log(tanh(G/kT/m))*1.0/kT
if exp(-dE/kT)>random.rand():
q[y][x] = -q[y][x]
G*=tau

E = 0
for a in range(N):
E += h[a]*q[0][a]
for b in range(a+1,N):
E += J[a][b]*q[0][a]*q[0][b]
qarr.append(q)
Earr.append(E+c)

return(array([qarr,Earr]))
@@ -0,0 +1,17 @@
# -*- coding:utf-8 -*-

from quantpy.ising import qubo
import numpy as np

#params are temperature, Gamma, trotter number, qubo matrix, number of trials
def test_qubo():
mat = np.array([[-32,4,4,4,4],[4,-32,4,4,4],[4,4,-32,4,4],[4,4,4,-32,4],[4,4,4,4,-32]])
assert all([a == b for a, b in zip(qubo.getMat(mat) ,
[[0.0, 0.0, 0.0, 0.0, 0.0],
[[0, 1.0, 1.0, 1.0, 1.0],
[1.0, 0, 1.0, 1.0, 1.0],
[1.0, 1.0, 0, 1.0, 1.0],
[1.0, 1.0, 1.0, 0, 1.0],
[1.0, 1.0, 1.0, 1.0, 0]],
-70.0])
])
@@ -0,0 +1,15 @@
# -*- coding:utf-8 -*-

"""
"""
from quantpy.ising import sqa, qubo
import numpy as np

#params are temperature, Gamma, trotter number, qubo matrix, number of trials
def test_qubo_sqa():
mat = np.array([[-32,4,4,4,4,4,4,4],[4,-32,4,4,4,4,4,4],[4,4,-32,4,4,4,4,4],[4,4,4,-32,4,4,4,4],[4,4,4,4,-32,4,4,4],[4,4,4,4,4,-32,4,4],[4,4,4,4,4,4,-32,4],[4,4,4,4,4,4,4,-32]])
arr = sqa.run(0.02,5,4,qubo.getMat(mat),10)
print(arr)
for j in range(8):
for i in range(4):
assert all([a == b for a, b in zip(arr[0][j][i], [1,1,1,1,1,1,1,1])])
@@ -1 +1,3 @@
__version__ = "0.0.1.dev1"
# -*- coding:utf-8 -*-

__version__ = "0.0.2"
@@ -0,0 +1,22 @@
# -*- coding:utf-8 -*-

__all__ = []

# The following pattern is used below for importing sub-modules:
#
# 1. "from foo import *". This imports all the names from foo.__all__ into
# this module. But, this does not put those names into the __all__ of
# this module. This enables "from quantpy.sympy import State" to
# work.
# 2. "import foo; __all__.extend(foo.__all__)". This adds all the names in
# foo.__all__ to the __all__ of this module. The names in __all__
# determine which names are imported when
# "from quantpy.sympy import *" is done.

#from .qapply import __all__ as qap_all
#from .qapply import *
#__all__.extend(qap_all)

#from . import _quantumexecutor
#from ._quantumexecutor import *
#__all__.extend(_quantumexecutor.__all__)
@@ -0,0 +1 @@
# -*- coding:utf-8 -*-
@@ -0,0 +1,63 @@
# -*- coding:utf-8 -*-
"""definition of BaseQuantumExecutor class
"""
from abc import abstractmethod

import sympy
from qiskit.qasm._qasmparser import QasmParser

class BaseQuantumExecutor:
"""BaseQuantumExecutor Class
"""
def __init__(self):
"""Initial method. No implements.
"""
pass

@abstractmethod
def execute(self, circuit, **options):
"""Abstract method.
@return: None
"""
return None

def to_qasm(self, sympy_expr):
"""QuantumExecutor classes' commom method.
Transform SymPy expression to OpenQASM format descriptions.
@return qasm format string.
"""
qasm = 'OPENQASM 2.0;\ninclude "qelib1.inc";\n'
assert isinstance(sympy_expr, sympy.mul.Mul), 'Sorry. Now, supported U*U*U*Qubit format'
qubit = sympy_expr.args[-1]
assert isinstance(qubit, sympy.physics.quantum.qubit.Qubit), 'Sorry. Now, supported U*U*U*Qubit format'
qasm += 'qreg qr[{0}];\ncreg cr[{0}];\n'.format(len(qubit))
for i, qb in enumerate(reversed(qubit.args)):
if isinstance(qb, sympy.numbers.One):
qasm += 'x qr[{}];\n'.format(i)
for gate in reversed(sympy_expr.args[:-1]):
if isinstance(gate, sympy.physics.quantum.gate.IdentityGate):
continue
elif isinstance(gate, sympy.physics.quantum.gate.HadamardGate):
qasm += 'h qr[{}];\n'.format(int(gate.args[0]))
elif isinstance(gate, sympy.physics.quantum.gate.XGate):
qasm += 'x qr[{}];\n'.format(int(gate.args[0]))
elif isinstance(gate, sympy.physics.quantum.gate.YGate):
qasm += 'y qr[{}];\n'.format(int(gate.args[0]))
elif isinstance(gate, sympy.physics.quantum.gate.ZGate):
qasm += 'z qr[{}];\n'.format(int(gate.args[0]))
elif isinstance(gate, sympy.physics.quantum.gate.PhaseGate):
qasm += 's qr[{}];\n'.format(int(gate.args[0]))
elif isinstance(gate, sympy.physics.quantum.gate.TGate):
qasm += 't qr[{}];\n'.format(int(gate.args[0]))
elif isinstance(gate, sympy.physics.quantum.gate.CNotGate):
qasm += 'cx qr[{}], qr[{}];\n'.format(int(gate.args[0]), int(gate.args[1]))
elif isinstance(gate, sympy.physics.quantum.gate.SwapGate):
qasm += 'cx qr[{}], qr[{}];\n'.format(int(gate.args[0]), int(gate.args[1]))
qasm += 'cx qr[{}], qr[{}];\n'.format(int(gate.args[1]), int(gate.args[0]))
qasm += 'cx qr[{}], qr[{}];\n'.format(int(gate.args[0]), int(gate.args[1]))
else:
assert False, '{} it is not a gate operator, nor is a supported operator'.format(repr(gate))
for i in range(len(qubit)):
qasm += 'measure qr[{0}] -> cr[{0}];\n'.format(i)
return qasm
Oops, something went wrong.

0 comments on commit c2f0ec0

Please sign in to comment.
You can’t perform that action at this time.