This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Advanced Compiler Features
Marc Davis edited this page Apr 30, 2020
·
9 revisions
The SearchCompiler
class takes a few optional initializer options.
mycompiler = sc.SearchCompiler.init(threshold, error_func, error_jac, eval_func, heuristic, gateset, solver, beams, logger, verbosity)
-
threshold
- a float which specifies the stopping condition for the synthesis. The first circuit for which theerror_func
returns a value less than thethreshold
will be returned as the solution. The default value is1e-10
. -
error_func
- a function which takes two matrices and returns a float. Its value is used in guiding the numerical solver. The default value issc.utils.matrix_distance_squared
. Usually, its value is also used foreval_func
. Ifeval_func
is specified separately,error_func
can behave differently, such as returning an array of residuals instead of a single value when using LeastSquares. -
error_jac
- a function which takes two matrices, and returns a tuple of the float that would be returned byerror_func
, and the Jacobian of that function. It's value is used by solvers that can take advantage of the Jacobian, such as BFGS and LeastSquares. The default value isNone
, and ifNone
is specified, an appropriate value will be chosen based on the value oferror_func
. -
eval_func
- a function which takes two matrices and returns a float. Whileerror_func
is used to guide the numerical solver,eval_func
is used to guide the search and decide when to return a solution. Its default value isNone
, and an appropriate value will be chosen at runtime. Specifically, it will be set tosc.utils.matrix_distance_squared
iferror_func
isutils.matrix_residuals
, and will be set toerror_func
otherwise. It is necessary to manually change this to use LeastSquares with a customerror_func
. -
heuristic
- a function which takes the value oferror_func
and the current search depth and returns a float. This value is used to guide the search. The default issc.heuristics.astar
. Heuristics for greedy search and breadth first search are also provided. It is recommended to switch to breadth first search if you are using a gateset with gates other than CNOT and single qubit gates. -
gateset
- asc.gatesets.Gateset
object which describes the gateset used for synthesis. The default value issc.gatesets.QubitCNOTLinear()
, which is the recommended gateset for using CNOTs and single qubit gates with the linear topology. -
solver
- asc.solver.Solver
object which performs numerical optimization. The default value isLeastSquares_Jac_Solver
ifgateset
is one of the provided gatesets consisting ofCNOT
and single qubit gates, orCOBYLA_Solver
otherwise. If the Native Gateset is installed, it will be used if it is supported by the givengateset
. -
beams
- an integer which defines the number of nodes to expand at a time during each step. A negative value will result in a number of beams calculated to fully utilize the CPU threads visible to Python. The default value is-1
. -
logger
- ansc.logging.Logger
object that the compiler will use for logging. The default value isNone
. See the wiki page for more information. -
verbosity
- iflogger
is left asNone
, then a logger will be created that prints to stdout with the specified verbosity. The default value is0
. A value of0
will silence all output, a value of1
will produce a default level of output, and a value of2
will produce more detailed output. See the wiki page for more information.
The compile
function of the SearchCompiler
class also can take two extra parameters:
circuit, vector = mycompiler.compile(U, depth, statefile, logger)
-
U
- the only required option, which is the unitary to be synthesized -
depth
- an integer which serves as a depth limit. If a circuit is not found within this limit that satisfies thethreshold
condition, then the circuit with the lowest value oferror_func
will be returned. The default value ofNone
will result in no depth limit being used, and the search will continue indefinitely until a circuit that meets thethreshold
requirement is found. -
statefile
- a string that describes a file path that will be used to store the intermediate state of the compilation. If the compilation is interrupted, it can be resumed by making the same call again, and the state will be restored from the statefile. Without a statefile, compilataion will have to start from the beginning if compilation is interrupted. -
logger
- asc.logging.Logger
object that can be used to override the logging settings provided on creating theSearchCompiler
object. The default value isNone, which causes the logger defined when creating the
SearchCompiler` object to be used. See the wiki page for more information.
The compile
function of the SearchCompiler
returns a tuple of two values:
circuit, vector = mycompiler.compile(U, depth, statefile, logger)
-
circuit
- This is asc.circuits.QuantumStep
object that contains the final solution circuit structure found by the compiler. -
vector
- This is anumpy
ndarray
object containing the final solution parameters found by the compiler. These two return values together describe the final circuit, and can be used to generate the implemented matrix or export the circuit to other formats:
U = circuit.matrix(vector) # returns the unitary matrix implemented by the circuit
sc.assembler.assemble(circuit, vector, language, write_location) # assembles the circuit and exports to another format
See the wiki page on assembling and exporting for more details.