Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
konakaji committed May 13, 2023
0 parents commit 4513d68
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 0 deletions.
153 changes: 153 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# Created by .ignore support plugin (hsz.mobi)
### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# IPython Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# dotenv
.env

# virtualenv
venv/
ENV/

# Spyder project settings
.spyderproject

# Rope project settings
.ropeproject
### VirtualEnv template
# Virtualenv
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
.Python
[Bb]in
[Ii]nclude
[Ll]ib
[Ll]ib64
[Ll]ocal
[Ss]cripts
pyvenv.cfg
.venv
pip-selfcheck.json
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea/workspace.xml
.idea/tasks.xml
.idea/dictionaries
.idea/vcs.xml
.idea/jsLibraryMappings.xml

# Sensitive or high-churn files:
.idea/dataSources.ids
.idea/dataSources.xml
.idea/dataSources.local.xml
.idea/sqlDataSources.xml
.idea/dynamic.xml
.idea/uiDesigner.xml

# Gradle:
.idea/gradle.xml
.idea/libraries

# Mongo Explorer plugin:
.idea/mongoSettings.xml

.idea/

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

ibmq/.ibmq_key
Empty file added qswiftencoder/__init__.py
Empty file.
73 changes: 73 additions & 0 deletions qswiftencoder/encoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from abc import ABC
from qwrapper.circuit import QWrapper
from qwrapper.operator import PauliTimeEvolution


class Operator(ABC):
pass


class TimeOperator(Operator):
def __init__(self, j):
self.j = j

def __repr__(self) -> str:
return "T" + str(self.j)


class SwiftOperator(Operator):
def __init__(self, j, b):
self.j = j
self.b = b

def __repr__(self):
return f"S{self.j}-{self.b}"


class QSwiftStringEncoder:
def encode(self, coeff, operators):
res = [str(coeff)]
for o in operators:
res.append(str(o))
return " ".join(res)


class QSwiftCircuitEncoder:
def __init__(self, ancilla_index, targets, paulis, tau):
self._ancilla_index = ancilla_index
self._targets = targets
self._paulis = paulis
self._tau = tau
self._cache = {}

def encode(self, qc: QWrapper, code):
operators = []
items = code.split(" ")
coeff = float(items[0])
for s in items[1:]:
if s.startswith("T"):
operators.append(TimeOperator(int(s[1:])))
elif s.startswith("S"):
j, b = s[1:].split("-")
operators.append(SwiftOperator(int(j), int(b)))
for operator in operators:
self.add_gate(qc, operator, self._tau)
return coeff, qc

def add_gate(self, qc: QWrapper, operator: Operator, tau):
if isinstance(operator, SwiftOperator):
qc.s(self._ancilla_index)
pauli = self._paulis[operator.j]
if pauli.sign == -1:
qc.z(self._ancilla_index)
if operator.b == 0:
qc.z(self._ancilla_index)
qc.x(self._ancilla_index)
pauli.add_controlled_circuit(self._ancilla_index, self._targets, qc)
qc.x(self._ancilla_index)
else:
pauli.add_controlled_circuit(self._ancilla_index, self._targets, qc)
elif isinstance(operator, TimeOperator):
if operator.j not in self._cache:
self._cache[operator.j] = PauliTimeEvolution(self._paulis[operator.j], tau)
self._cache[operator.j].add_circuit(qc)
24 changes: 24 additions & 0 deletions qswiftencoder/test_encoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from unittest import TestCase
from qswiftencoder.encoder import QSwiftCircuitEncoder, QSwiftStringEncoder, TimeOperator, SwiftOperator
from qwrapper.operator import PauliObservable
from qwrapper.circuit import init_circuit


class TestQSwiftCircuitEncoder(TestCase):
def test_encode(self):
operators = []
for _ in range(10):
operators.append(TimeOperator(0))
operators.append(SwiftOperator(0, 0))
s_encoder = QSwiftStringEncoder()
code = s_encoder.encode(2.2, operators)
self.assertEquals("2.2 T0 T0 T0 T0 T0 T0 T0 T0 T0 T0 S0-0", code)

paulis = [PauliObservable("XXXIIIII")]
encoder = QSwiftCircuitEncoder(0, [1, 2, 3, 4, 5, 6, 7, 8], paulis, 0.1)

qc = init_circuit(9, "qulacs")
sign, qc = encoder.encode(qc, code)

obs = PauliObservable("XZZZIIIII")
self.assertEquals(0, obs.exact_value(qc))
29 changes: 29 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import setuptools

with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup(
name="qwrapper",
version="0.4.9",
author="kouhei nakaji",
author_email="[email protected]",
description="You can receive the message 'Hello!!!'",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/konakaji/qwrapper",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
install_requires=[
"qiskit>=0.30.0",
"Qulacs>=0.3.0",
"matplotlib>=3.0.0",
"pylatexenc>=2.0",
"qutip>=4.7.0"
],
python_requires='>=3.6',
)

0 comments on commit 4513d68

Please sign in to comment.