Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Make changes for packaging, optional solvers #1

Merged
merged 1 commit into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added README.md
Empty file.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["wheel", "setuptools"]
build-backend = "setuptools.build_meta"
4 changes: 2 additions & 2 deletions search_compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .circuits import *

from . import gatesets as gatesets
from .solver import CMA_Solver
from .solver import default_solver
from .logging import logprint
from . import checkpoint, utils, heuristics

Expand All @@ -20,7 +20,7 @@ def evaluate_step(tup, U, error_func, solver):
return (step, solver.solve_for_unitary(step, U, error_func, initial_guess), depth)

class SearchCompiler(Compiler):
def __init__(self, threshold=0.01, d=2, error_func=utils.matrix_distance_squared, heuristic=heuristics.astar, gateset=gatesets.Default(), solver=CMA_Solver(), beams=1):
def __init__(self, threshold=0.01, d=2, error_func=utils.matrix_distance_squared, heuristic=heuristics.astar, gateset=gatesets.Default(), solver=default_solver(), beams=1):
self.threshold = threshold
self.error_func = error_func
self.heuristic = heuristic
Expand Down
15 changes: 13 additions & 2 deletions search_compiler/graphics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import matplotlib
import matplotlib.pyplot
import sys

import numpy as np

try:
import matplotlib
import matplotlib.pyplot
HAVE_MATPLOTLIB = True
except ImportError:
HAVE_MATPLOTLIB = False


# This code was written by Rick Muller "Simplified Plotting Routines for Quantum Circuits"
# https://github.com/rpmuller/PlotQCircuit/blob/master/PlotQCircuit.ipynb

Expand All @@ -14,6 +22,9 @@ def plot_quantum_circuit(gates,inits={},labels=[],plot_labels=True,**kwargs):

kwargs Can override plot_parameters
"""
if not HAVE_MATPLOTLIB:
print("ERROR: Could not find matplotlib, try running pip install quantum_synthesis[graphics]", file=sys.stderr)
sys.exit(1)
plot_params = dict(scale = 1.0,fontsize = 14.0, linewidth = 1.0,
control_radius = 0.05, not_radius = 0.15,
swap_delta = 0.08, label_buffer = 0.0)
Expand Down
4 changes: 2 additions & 2 deletions search_compiler/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import shutil
import pickle
from .compiler import SearchCompiler
from .solver import CMA_Solver
from .solver import default_solver
from . import logging, checkpoint, utils, gatesets, heuristics, assembler

PROJECT_STATUS_PROGRESS = 1
Expand Down Expand Up @@ -98,7 +98,7 @@ def run(self, target=None):
heuristic = heuristics.greedy

heuristic = self._config("heuristic", heuristic)
solver = self._config("solver", CMA_Solver())
solver = self._config("solver", default_solver())
beams = self._config("beams", 1)
compiler = SearchCompiler(threshold=threshold, d=d, gateset=gateset, error_func=error_func, heuristic=heuristic, solver=solver, beams=beams)
self.status()
Expand Down
39 changes: 36 additions & 3 deletions search_compiler/solver.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
import sys

import numpy as np
import cma
import scipy as sp
import scipy.optimize

from . import circuits as circuits
from . import utils as util


def default_solver():
try:
import cma
except ImportError:
pass
else:
return CMA_Solver()
try:
import scipy
except ImportError:
pass
else:
return COBYLA_Solver()
print(
"ERROR: Could not find a solver library, please install one via pip install quantum_synthesis[<solver>] where <solver> is one of cma, cobyla, or bfgs.", file=sys.stderr)
sys.exit(1)

class CMA_Solver():
def solve_for_unitary(self, circuit, U, error_func=util.matrix_distance_squared, initial_guess=None):
try:
import cma
except ImportError:
print("ERROR: Could not find cma, try running pip install quantum_synthesis[cma]", file=sys.stderr)
sys.exit(1)
eval_func = lambda v: error_func(U, circuit.matrix(v))
if initial_guess is None:
initial_guess = 'np.random.rand({})'.format(circuit.num_inputs)
Expand All @@ -20,13 +41,25 @@ def solve_for_unitary(self, circuit, U, error_func=util.matrix_distance_squared,

class BFGS_Solver():
def solve_for_unitary(self, circuit, U, error_func=util.matrix_distance_squared, initial_guess=None):
try:
import scipy as sp
import scipy.optimize
except ImportError:
print("ERROR: Could not find scipy, try running pip install quantum_synthesis[bfgs]", file=sys.stderr)
sys.exit(1)
eval_func = lambda v: error_func(U, circuit.matrix(v))
result = sp.optimize.minimize(eval_func, np.random.rand(circuit.num_inputs)*np.pi, method='BFGS', bounds=[(0, 1) for _ in range(0, circuit.num_inputs)])
xopt = result.x
return (circuit.matrix(xopt), xopt)

class COBYLA_Solver():
def solve_for_unitary(self, circuit, U, error_func=util.matrix_distance_squared, initial_guess=None):
try:
import scipy as sp
import scipy.optimize
except ImportError:
print("ERROR: Could not find scipy, try running pip install quantum_synthesis[cobyla]", file=sys.stderr)
sys.exit(1)
eval_func = lambda v: error_func(U, circuit.matrix(v))
if initial_guess is None:
initial_guess = []
Expand Down
32 changes: 32 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from setuptools import setup
from pathlib import Path

README = Path('README.md').read_text()

setup(
name="quantum_synthesis",
version="0.1.0",
description="Quantum Synthesis",
long_description=README,
long_description_content_type="text/markdown",
author="LBNL - AQT",
author_email="[email protected]",
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Science/Research',
'Topic :: Software Development :: Compilers',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
],
keywords='quantum compilers synthesis computing',
packages=['search_compiler'],
python_requires='>=3.6, <4',
install_requires=['numpy'],
extras_require={
'cma': ['cma'],
'cobyla': ['scipy'],
'bfgs': ['scipy'],
'graphics': ['matplotlib'],
},
)