From 4513d6835841c7563c51fc6cced7b872120d7f47 Mon Sep 17 00:00:00 2001 From: knakaji Date: Sat, 13 May 2023 19:21:55 -0400 Subject: [PATCH] first commit --- .gitignore | 153 ++++++++++++++++++++++++++++++++++ qswiftencoder/__init__.py | 0 qswiftencoder/encoder.py | 73 ++++++++++++++++ qswiftencoder/test_encoder.py | 24 ++++++ setup.py | 29 +++++++ 5 files changed, 279 insertions(+) create mode 100644 .gitignore create mode 100644 qswiftencoder/__init__.py create mode 100644 qswiftencoder/encoder.py create mode 100644 qswiftencoder/test_encoder.py create mode 100644 setup.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ab2518d --- /dev/null +++ b/.gitignore @@ -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 \ No newline at end of file diff --git a/qswiftencoder/__init__.py b/qswiftencoder/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/qswiftencoder/encoder.py b/qswiftencoder/encoder.py new file mode 100644 index 0000000..d07be37 --- /dev/null +++ b/qswiftencoder/encoder.py @@ -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) diff --git a/qswiftencoder/test_encoder.py b/qswiftencoder/test_encoder.py new file mode 100644 index 0000000..71584ab --- /dev/null +++ b/qswiftencoder/test_encoder.py @@ -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)) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..c020323 --- /dev/null +++ b/setup.py @@ -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="nakajijiji@gmail.com", + 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', +)