Function Reference

SdpRelaxation Class

class ncpol2sdpa.SdpRelaxation(variables, parameters=None, verbose=0, normalized=True, parallel=False)

Class for obtaining sparse SDP relaxation.

Parameters:
  • variables (list of sympy.physics.quantum.operator.Operator or sympy.physics.quantum.operator.HermitianOperator or a list of list.) – Commutative or noncommutative, Hermitian or nonhermiatian variables, possibly a list of list of variables if the hierarchy is not NPA.
  • parameters (list of sympy.physics.quantum.operator.Operator or sympy.physics.quantum.operator.HermitianOperator or a list of list.) – Optional symbolic variables for which moments are not generated.
  • verbose (int.) –

    Optional parameter for level of verbosity:

    • 0: quiet (default)
    • 1: verbose
    • 2: debug level
  • normalized (bool.) – Optional parameter for changing the normalization of states over which the optimization happens. Turn it off if further processing is done on the SDP matrix before solving it.
  • parallel (bool.) – Optional parameter for allowing parallel computations.
Attributes:
  • monomial_sets: The monomial sets that generate the moment matrix blocks.
  • monomial_index: Dictionary that maps monomials to SDP variables.
  • constraints: The complete set of constraints after preprocesssing.
  • primal: The primal optimal value.
  • dual: The dual optimal value.
  • x_mat: The primal solution matrix.
  • y_mat: The dual solution matrix.
  • solution_time: The amount of time taken to solve the relaxation.
  • status: The solution status of the relaxation.
__getitem__(index)

Obtained the value for a polynomial in a solved relaxation.

Parameters:index (sympy.core.exp.Expr) – The polynomial.
Returns:The value of the polynomial extracted from the solved SDP.
Return type:float
convert_to_mosek()

Convert an SDP relaxation to a MOSEK task.

Returns:mosek.Task.
convert_to_picos(duplicate_moment_matrix=False)

Convert the SDP relaxation to a PICOS problem such that the exported .dat-s file is extremely sparse, there is not penalty imposed in terms of SDP variables or number of constraints. This conversion can be used for imposing extra constraints on the moment matrix, such as partial transpose.

Parameters:duplicate_moment_matrix (bool.) – Optional parameter to add an unconstrained moment matrix to the problem with the same structure as the moment matrix with the PSD constraint.
Returns:picos.Problem.
extract_dual_value(monomial, blocks=None)

Given a solution of the dual problem and a monomial, it returns the inner product of the corresponding coefficient matrix and the dual solution. It can be restricted to certain blocks.

Parameters:
  • monomial (sympy.core.exp.Expr.) – The monomial for which the value is requested.
  • monomial – The monomial for which the value is requested.
  • blocks (list of int.) – Optional parameter to specify the blocks to be included.
Returns:

The value of the monomial in the solved relaxation.

Return type:

float.

find_solution_ranks(xmat=None, baselevel=0)

Helper function to detect rank loop in the solution matrix.

Parameters:
  • sdpRelaxation (ncpol2sdpa.SdpRelaxation.) – The SDP relaxation.
  • x_mat (numpy.array.) – Optional parameter providing the primal solution of the moment matrix. If not provided, the solution is extracted from the sdpRelaxation object.
  • base_level (int.) – Optional parameter for specifying the lower level relaxation for which the rank loop should be tested against.
Returns:

list of int – the ranks of the solution matrix with in the order of increasing degree.

get_relaxation(level, objective=None, inequalities=None, equalities=None, substitutions=None, momentinequalities=None, momentequalities=None, momentsubstitutions=None, removeequalities=False, extramonomials=None, extramomentmatrices=None, extraobjexpr=None, localizing_monomials=None, chordal_extension=False)

Get the SDP relaxation of a noncommutative polynomial optimization problem.

Parameters:
  • level (int.) – The level of the relaxation. The value -1 will skip automatic monomial generation and use only the monomials supplied by the option extramonomials.
  • obj (sympy.core.exp.Expr.) – Optional parameter to describe the objective function.
  • inequalities (list of sympy.core.exp.Expr.) – Optional parameter to list inequality constraints.
  • equalities (list of sympy.core.exp.Expr.) – Optional parameter to list equality constraints.
  • substitutions (dict of sympy.core.exp.Expr.) – Optional parameter containing monomials that can be replaced (e.g., idempotent variables).
  • momentinequalities (list of sympy.core.exp.Expr.) – Optional parameter of inequalities defined on moments.
  • momentequalities (list of sympy.core.exp.Expr.) – Optional parameter of equalities defined on moments.
  • momentsubstitutions (dict of sympy.core.exp.Expr.) – Optional parameter containing moments that can be replaced.
  • removeequalities (bool.) – Optional parameter to attempt removing the equalities by solving the linear equations.
  • extramonomials (list of sympy.core.exp.Expr.) – Optional paramter of monomials to be included, on top of the requested level of relaxation.
  • extramomentmatrices (list of list of str.) – Optional paramter of duplicating or adding moment matrices. A new moment matrix can be unconstrained (“”), a copy of the first one (“copy”), and satisfying a partial positivity constraint (“ppt”). Each new moment matrix is requested as a list of string of these options. For instance, adding a single new moment matrix as a copy of the first would be extramomentmatrices=[["copy"]].
  • extraobjexpr (str.) – Optional parameter of a string expression of a linear combination of moment matrix elements to be included in the objective function.
  • localizing_monomials (list of list of sympy.core.exp.Expr.) – Optional parameter to specify sets of localizing monomials for each constraint. The internal order of constraints is inequalities first, followed by the equalities. If the parameter is specified, but for a certain constraint the automatic localization is requested, leave None in its place in this parameter.
  • chordal_extension (bool.) – Optional parameter to request a sparse chordal extension.
get_sos_decomposition(threshold=0.0)

Given a solution of the dual problem, it returns the SOS decomposition.

Parameters:threshold (float.) – Optional parameter for specifying the threshold value below which the eigenvalues and entries of the eigenvectors are disregarded.
Returns:The SOS decomposition of [sigma_0, sigma_1, …, sigma_m]
Return type:list of sympy.core.exp.Expr.
process_constraints(inequalities=None, equalities=None, momentinequalities=None, momentequalities=None, block_index=0, removeequalities=False)

Process the constraints and generate localizing matrices. Useful only if the moment matrix already exists. Call it if you want to replace your constraints. The number of the respective types of constraints and the maximum degree of each constraint must remain the same.

Parameters:
  • inequalities (list of sympy.core.exp.Expr.) – Optional parameter to list inequality constraints.
  • equalities (list of sympy.core.exp.Expr.) – Optional parameter to list equality constraints.
  • momentinequalities (list of sympy.core.exp.Expr.) – Optional parameter of inequalities defined on moments.
  • momentequalities (list of sympy.core.exp.Expr.) – Optional parameter of equalities defined on moments.
  • removeequalities (bool.) – Optional parameter to attempt removing the equalities by solving the linear equations.
  • removeequalities – Optional parameter to attempt removing the equalities by solving the linear equations.
save_monomial_index(filename)

Write the monomial index to a file.

Parameters:filename (str.) – The name of the file to write to.
set_objective(objective, extraobjexpr=None)

Set or change the objective function of the polynomial optimization problem.

Parameters:
  • objective (sympy.core.expr.Expr) – Describes the objective function.
  • extraobjexpr (str.) – Optional parameter of a string expression of a linear combination of moment matrix elements to be included in the objective function
solve(solver=None, solverparameters=None)

Call a solver on the SDP relaxation. Upon successful solution, it returns the primal and dual objective values along with the solution matrices. It also sets these values in the sdpRelaxation object, along with some status information.

Parameters:
  • sdpRelaxation (ncpol2sdpa.SdpRelaxation.) – The SDP relaxation to be solved.
  • solver (str.) – The solver to be called, either None, “sdpa”, “mosek”, “cvxpy”, “scs”, or “cvxopt”. The default is None, which triggers autodetect.
  • solverparameters (dict of str.) –

    Parameters to be passed to the solver. Actual options depend on the solver:

    SDPA:

    • ”executable”: Specify the executable for SDPA. E.g., “executable”:”/usr/local/bin/sdpa”, or “executable”:”sdpa_gmp”
    • ”paramsfile”: Specify the parameter file

    Mosek: Refer to the Mosek documentation. All arguments are passed on.

    Cvxopt: Refer to the PICOS documentation. All arguments are passed on.

    Cvxpy: Refer to the Cvxpy documentation. All arguments are passed on.

    SCS: Refer to the Cvxpy documentation. All arguments are passed on.

write_to_file(filename, filetype=None)

Write the relaxation to a file.

Parameters:
  • filename (str.) – The name of the file to write to. The type can be autodetected from the extension: .dat-s for SDPA, .task for mosek or .csv for human readable format.
  • filetype (str.) – Optional parameter to define the filetype. It can be “sdpa” for SDPA , “mosek” for Mosek, or “csv” for human readable format.

MoroderHierarchy Class

class ncpol2sdpa.SteeringHierarchy(variables, verbose=0, matrix_var_dim=None, mark_conjugate=False, parallel=False)

Class for obtaining a step in the steering hierarchy.

Parameters:
  • variables (list of sympy.physics.quantum.operator.Operator or sympy.physics.quantum.operator.HermitianOperator or a list of list.) – Commutative or noncommutative, Hermitian or nonhermiatian variables.
  • verbose (int.) –

    Optional parameter for level of verbosity:

    • 0: quiet
    • 1: verbose
    • 2: debug level
  • matrix_var_dim (int.) – Optional parameter to specify the size of matrix variable blocks
  • mark_conjugate (bool.) – Use this optional parameter to generate a symbolic representation of the steering hierarchy for export.
Attributes:
  • monomial_sets: The monomial sets that generate the moment matrix blocks.
  • monomial_index: Dictionary that maps monomials to SDP variables.
  • constraints: The complete set of constraints after preprocesssing.
  • primal: The primal optimal value.
  • dual: The dual optimal value.
  • x_mat: The primal solution matrix.
  • y_mat: The dual solution matrix.
  • solution_time: The amount of time taken to solve the relaxation.
  • status: The solution status of the relaxation.
__getitem__(index)

Obtained the value for a polynomial in a solved relaxation.

Parameters:index (sympy.core.exp.Expr) – The polynomial.
Returns:The value of the polynomial extracted from the solved SDP.
Return type:float
convert_to_mosek()

Convert an SDP relaxation to a MOSEK task.

Returns:mosek.Task.
convert_to_picos(duplicate_moment_matrix=False)

Convert the SDP relaxation to a PICOS problem such that the exported .dat-s file is extremely sparse, there is not penalty imposed in terms of SDP variables or number of constraints. This conversion can be used for imposing extra constraints on the moment matrix, such as partial transpose.

Parameters:duplicate_moment_matrix (bool.) – Optional parameter to add an unconstrained moment matrix to the problem with the same structure as the moment matrix with the PSD constraint.
Returns:picos.Problem.
get_relaxation(level, objective=None, inequalities=None, equalities=None, substitutions=None, momentinequalities=None, momentequalities=None, momentsubstitutions=None, removeequalities=False, extramonomials=None, extramomentmatrices=None, extraobjexpr=None, localizing_monomials=None, chordal_extension=False)

Get the SDP relaxation of a noncommutative polynomial optimization problem.

Parameters:
  • level (int.) – The level of the relaxation. The value -1 will skip automatic monomial generation and use only the monomials supplied by the option extramonomials.
  • obj (sympy.core.exp.Expr.) – Optional parameter to describe the objective function.
  • inequalities (list of sympy.core.exp.Expr.) – Optional parameter to list inequality constraints.
  • equalities (list of sympy.core.exp.Expr.) – Optional parameter to list equality constraints.
  • substitutions (dict of sympy.core.exp.Expr.) – Optional parameter containing monomials that can be replaced (e.g., idempotent variables).
  • momentinequalities (list of sympy.core.exp.Expr.) – Optional parameter of inequalities defined on moments.
  • momentequalities (list of sympy.core.exp.Expr.) – Optional parameter of equalities defined on moments.
  • momentsubstitutions (dict of sympy.core.exp.Expr.) – Optional parameter containing moments that can be replaced.
  • removeequalities (bool.) – Optional parameter to attempt removing the equalities by solving the linear equations.
  • extramonomials (list of sympy.core.exp.Expr.) – Optional paramter of monomials to be included, on top of the requested level of relaxation.
  • extramomentmatrices (list of list of str.) – Optional paramter of duplicating or adding moment matrices. A new moment matrix can be unconstrained (“”), a copy of the first one (“copy”), and satisfying a partial positivity constraint (“ppt”). Each new moment matrix is requested as a list of string of these options. For instance, adding a single new moment matrix as a copy of the first would be extramomentmatrices=[["copy"]].
  • extraobjexpr (str.) – Optional parameter of a string expression of a linear combination of moment matrix elements to be included in the objective function.
  • localizing_monomials (list of list of sympy.core.exp.Expr.) – Optional parameter to specify sets of localizing monomials for each constraint. The internal order of constraints is inequalities first, followed by the equalities. If the parameter is specified, but for a certain constraint the automatic localization is requested, leave None in its place in this parameter.
  • chordal_extension (bool.) – Optional parameter to request a sparse chordal extension.
process_constraints(inequalities=None, equalities=None, momentinequalities=None, momentequalities=None, block_index=0, removeequalities=False)

Process the constraints and generate localizing matrices. Useful only if the moment matrix already exists. Call it if you want to replace your constraints. The number of the respective types of constraints and the maximum degree of each constraint must remain the same.

Parameters:
  • inequalities (list of sympy.core.exp.Expr.) – Optional parameter to list inequality constraints.
  • equalities (list of sympy.core.exp.Expr.) – Optional parameter to list equality constraints.
  • momentinequalities (list of sympy.core.exp.Expr.) – Optional parameter of inequalities defined on moments.
  • momentequalities (list of sympy.core.exp.Expr.) – Optional parameter of equalities defined on moments.
  • removeequalities (bool.) – Optional parameter to attempt removing the equalities by solving the linear equations.
  • removeequalities – Optional parameter to attempt removing the equalities by solving the linear equations.
save_monomial_index(filename)

Write the monomial index to a file.

Parameters:filename (str.) – The name of the file to write to.
set_objective(objective, extraobjexpr=None)

Set or change the objective function of the polynomial optimization problem.

Parameters:
  • objective (sympy.core.expr.Expr) – Describes the objective function.
  • extraobjexpr (str.) – Optional parameter of a string expression of a linear combination of moment matrix elements to be included in the objective function
solve(solver=None, solverparameters=None)

Call a solver on the SDP relaxation. Upon successful solution, it returns the primal and dual objective values along with the solution matrices. It also sets these values in the sdpRelaxation object, along with some status information.

Parameters:
  • sdpRelaxation (ncpol2sdpa.SdpRelaxation.) – The SDP relaxation to be solved.
  • solver (str.) – The solver to be called, either None, “sdpa”, “mosek”, “cvxpy”, “scs”, or “cvxopt”. The default is None, which triggers autodetect.
  • solverparameters (dict of str.) –

    Parameters to be passed to the solver. Actual options depend on the solver:

    SDPA:

    • ”executable”: Specify the executable for SDPA. E.g., “executable”:”/usr/local/bin/sdpa”, or “executable”:”sdpa_gmp”
    • ”paramsfile”: Specify the parameter file

    Mosek: Refer to the Mosek documentation. All arguments are passed on.

    Cvxopt: Refer to the PICOS documentation. All arguments are passed on.

    Cvxpy: Refer to the Cvxpy documentation. All arguments are passed on.

    SCS: Refer to the Cvxpy documentation. All arguments are passed on.

write_to_file(filename, filetype=None)

Write the relaxation to a file.

Parameters:
  • filename (str.) – The name of the file to write to. The type can be autodetected from the extension: .dat-s for SDPA, .task for mosek, .csv for human readable format, or .txt for a symbolic export
  • filetype (str.) – Optional parameter to define the filetype. It can be “sdpa” for SDPA , “mosek” for Mosek, “csv” for human readable format, or “txt” for a symbolic export.

SteeringHierarchy Class

class ncpol2sdpa.SteeringHierarchy(variables, verbose=0, matrix_var_dim=None, mark_conjugate=False, parallel=False)

Class for obtaining a step in the steering hierarchy.

Parameters:
  • variables (list of sympy.physics.quantum.operator.Operator or sympy.physics.quantum.operator.HermitianOperator or a list of list.) – Commutative or noncommutative, Hermitian or nonhermiatian variables.
  • verbose (int.) –

    Optional parameter for level of verbosity:

    • 0: quiet
    • 1: verbose
    • 2: debug level
  • matrix_var_dim (int.) – Optional parameter to specify the size of matrix variable blocks
  • mark_conjugate (bool.) – Use this optional parameter to generate a symbolic representation of the steering hierarchy for export.
Attributes:
  • monomial_sets: The monomial sets that generate the moment matrix blocks.
  • monomial_index: Dictionary that maps monomials to SDP variables.
  • constraints: The complete set of constraints after preprocesssing.
  • primal: The primal optimal value.
  • dual: The dual optimal value.
  • x_mat: The primal solution matrix.
  • y_mat: The dual solution matrix.
  • solution_time: The amount of time taken to solve the relaxation.
  • status: The solution status of the relaxation.
__getitem__(index)

Obtained the value for a polynomial in a solved relaxation.

Parameters:index (sympy.core.exp.Expr) – The polynomial.
Returns:The value of the polynomial extracted from the solved SDP.
Return type:float
convert_to_mosek()

Convert an SDP relaxation to a MOSEK task.

Returns:mosek.Task.
convert_to_picos(duplicate_moment_matrix=False)

Convert the SDP relaxation to a PICOS problem such that the exported .dat-s file is extremely sparse, there is not penalty imposed in terms of SDP variables or number of constraints. This conversion can be used for imposing extra constraints on the moment matrix, such as partial transpose.

Parameters:duplicate_moment_matrix (bool.) – Optional parameter to add an unconstrained moment matrix to the problem with the same structure as the moment matrix with the PSD constraint.
Returns:picos.Problem.
get_relaxation(level, objective=None, inequalities=None, equalities=None, substitutions=None, momentinequalities=None, momentequalities=None, momentsubstitutions=None, removeequalities=False, extramonomials=None, extramomentmatrices=None, extraobjexpr=None, localizing_monomials=None, chordal_extension=False)

Get the SDP relaxation of a noncommutative polynomial optimization problem.

Parameters:
  • level (int.) – The level of the relaxation. The value -1 will skip automatic monomial generation and use only the monomials supplied by the option extramonomials.
  • obj (sympy.core.exp.Expr.) – Optional parameter to describe the objective function.
  • inequalities (list of sympy.core.exp.Expr.) – Optional parameter to list inequality constraints.
  • equalities (list of sympy.core.exp.Expr.) – Optional parameter to list equality constraints.
  • substitutions (dict of sympy.core.exp.Expr.) – Optional parameter containing monomials that can be replaced (e.g., idempotent variables).
  • momentinequalities (list of sympy.core.exp.Expr.) – Optional parameter of inequalities defined on moments.
  • momentequalities (list of sympy.core.exp.Expr.) – Optional parameter of equalities defined on moments.
  • momentsubstitutions (dict of sympy.core.exp.Expr.) – Optional parameter containing moments that can be replaced.
  • removeequalities (bool.) – Optional parameter to attempt removing the equalities by solving the linear equations.
  • extramonomials (list of sympy.core.exp.Expr.) – Optional paramter of monomials to be included, on top of the requested level of relaxation.
  • extramomentmatrices (list of list of str.) – Optional paramter of duplicating or adding moment matrices. A new moment matrix can be unconstrained (“”), a copy of the first one (“copy”), and satisfying a partial positivity constraint (“ppt”). Each new moment matrix is requested as a list of string of these options. For instance, adding a single new moment matrix as a copy of the first would be extramomentmatrices=[["copy"]].
  • extraobjexpr (str.) – Optional parameter of a string expression of a linear combination of moment matrix elements to be included in the objective function.
  • localizing_monomials (list of list of sympy.core.exp.Expr.) – Optional parameter to specify sets of localizing monomials for each constraint. The internal order of constraints is inequalities first, followed by the equalities. If the parameter is specified, but for a certain constraint the automatic localization is requested, leave None in its place in this parameter.
  • chordal_extension (bool.) – Optional parameter to request a sparse chordal extension.
process_constraints(inequalities=None, equalities=None, momentinequalities=None, momentequalities=None, block_index=0, removeequalities=False)

Process the constraints and generate localizing matrices. Useful only if the moment matrix already exists. Call it if you want to replace your constraints. The number of the respective types of constraints and the maximum degree of each constraint must remain the same.

Parameters:
  • inequalities (list of sympy.core.exp.Expr.) – Optional parameter to list inequality constraints.
  • equalities (list of sympy.core.exp.Expr.) – Optional parameter to list equality constraints.
  • momentinequalities (list of sympy.core.exp.Expr.) – Optional parameter of inequalities defined on moments.
  • momentequalities (list of sympy.core.exp.Expr.) – Optional parameter of equalities defined on moments.
  • removeequalities (bool.) – Optional parameter to attempt removing the equalities by solving the linear equations.
  • removeequalities – Optional parameter to attempt removing the equalities by solving the linear equations.
save_monomial_index(filename)

Write the monomial index to a file.

Parameters:filename (str.) – The name of the file to write to.
set_objective(objective, extraobjexpr=None)

Set or change the objective function of the polynomial optimization problem.

Parameters:
  • objective (sympy.core.expr.Expr) – Describes the objective function.
  • extraobjexpr (str.) – Optional parameter of a string expression of a linear combination of moment matrix elements to be included in the objective function
solve(solver=None, solverparameters=None)

Call a solver on the SDP relaxation. Upon successful solution, it returns the primal and dual objective values along with the solution matrices. It also sets these values in the sdpRelaxation object, along with some status information.

Parameters:
  • sdpRelaxation (ncpol2sdpa.SdpRelaxation.) – The SDP relaxation to be solved.
  • solver (str.) – The solver to be called, either None, “sdpa”, “mosek”, “cvxpy”, “scs”, or “cvxopt”. The default is None, which triggers autodetect.
  • solverparameters (dict of str.) –

    Parameters to be passed to the solver. Actual options depend on the solver:

    SDPA:

    • ”executable”: Specify the executable for SDPA. E.g., “executable”:”/usr/local/bin/sdpa”, or “executable”:”sdpa_gmp”
    • ”paramsfile”: Specify the parameter file

    Mosek: Refer to the Mosek documentation. All arguments are passed on.

    Cvxopt: Refer to the PICOS documentation. All arguments are passed on.

    Cvxpy: Refer to the Cvxpy documentation. All arguments are passed on.

    SCS: Refer to the Cvxpy documentation. All arguments are passed on.

write_to_file(filename, filetype=None)

Write the relaxation to a file.

Parameters:
  • filename (str.) – The name of the file to write to. The type can be autodetected from the extension: .dat-s for SDPA, .task for mosek, .csv for human readable format, or .txt for a symbolic export
  • filetype (str.) – Optional parameter to define the filetype. It can be “sdpa” for SDPA , “mosek” for Mosek, “csv” for human readable format, or “txt” for a symbolic export.

FaacetsRelaxation Class

class ncpol2sdpa.FaacetsRelaxation

Class for wrapping around a Faacets relaxation.

get_relaxation(A_configuration, B_configuration, I)

Get the sparse SDP relaxation of a Bell inequality.

Parameters:
  • A_configuration (list of list of int.) – The definition of measurements of Alice.
  • B_configuration (list of list of int.) – The definition of measurements of Bob.
  • I (list of list of int.) – The matrix describing the Bell inequality in the Collins-Gisin picture.
solve(solver=None, solverparameters=None)

Call a solver on the SDP relaxation. Upon successful solution, it returns the primal and dual objective values along with the solution matrices. It also sets these values in the sdpRelaxation object, along with some status information.

Parameters:
  • sdpRelaxation (ncpol2sdpa.SdpRelaxation.) – The SDP relaxation to be solved.
  • solver (str.) – The solver to be called, either None, “sdpa”, “mosek”, “cvxpy”, “scs”, or “cvxopt”. The default is None, which triggers autodetect.
  • solverparameters (dict of str.) –

    Parameters to be passed to the solver. Actual options depend on the solver:

    SDPA:

    • ”executable”: Specify the executable for SDPA. E.g., “executable”:”/usr/local/bin/sdpa”, or “executable”:”sdpa_gmp”
    • ”paramsfile”: Specify the parameter file

    Mosek: Refer to the Mosek documentation. All arguments are passed on.

    Cvxopt: Refer to the PICOS documentation. All arguments are passed on.

    Cvxpy: Refer to the Cvxpy documentation. All arguments are passed on.

    SCS: Refer to the Cvxpy documentation. All arguments are passed on.

Functions to Help Define Polynomial Optimization Problems

ncpol2sdpa.generate_operators(name, n_vars=1, hermitian=None, commutative=False)

Generates a number of commutative or noncommutative operators

Parameters:
  • name (str.) – The prefix in the symbolic representation of the noncommuting variables. This will be suffixed by a number from 0 to n_vars-1 if n_vars > 1.
  • n_vars (int.) – The number of variables.
  • hermitian (bool.) – Optional parameter to request Hermitian variables .
  • commutative (bool.) – Optional parameter to request commutative variables. Commutative variables are Hermitian by default.
Returns:

list of sympy.physics.quantum.operator.Operator or sympy.physics.quantum.operator.HermitianOperator variables

Example:
>>> generate_variables('y', 2, commutative=True)
[y0, y1]
ncpol2sdpa.generate_variables(name, n_vars=1, hermitian=None, commutative=True)

Generates a number of commutative or noncommutative variables

Parameters:
  • name (str.) – The prefix in the symbolic representation of the noncommuting variables. This will be suffixed by a number from 0 to n_vars-1 if n_vars > 1.
  • n_vars (int.) – The number of variables.
  • hermitian (bool.) – Optional parameter to request Hermitian variables .
  • commutative (bool.) – Optional parameter to request commutative variables. Commutative variables are Hermitian by default.
Returns:

list of sympy.physics.quantum.operator.Operator or sympy.physics.quantum.operator.HermitianOperator variables or sympy.Symbol

Example:
>>> generate_variables('y', 2, commutative=True)
[y0, y1]
ncpol2sdpa.get_monomials(variables, degree)

Generates all noncommutative monomials up to a degree

Parameters:
  • variables (list of sympy.physics.quantum.operator.Operator or sympy.physics.quantum.operator.HermitianOperator.) – The noncommutative variables to generate monomials from
  • degree (int.) – The maximum degree.
Returns:

list of monomials.

ncpol2sdpa.flatten(lol)

Flatten a list of lists to a list.

Parameters:lol (list of list.) – A list of lists in arbitrary depth.
Returns:flat list of elements.

Functions to Study Output of Solver

ncpol2sdpa.read_sdpa_out(filename, solutionmatrix=False, status=False, sdp=None)

Helper function to parse the output file of SDPA.

Parameters:
  • filename (str.) – The name of the SDPA output file.
  • solutionmatrix (bool.) – Optional parameter for retrieving the solution.
  • status (bool.) – Optional parameter for retrieving the status.
  • sdp (sdp.) – Optional parameter to add the solution to a relaxation.
Returns:

tuple of two floats and optionally two lists of numpy.array and a status string

Functions and Classes to Define Physics Problems

class ncpol2sdpa.Probability(*args, **kwargs)
__call__(output_, input_, marginal=None)

Obtain your probabilities in the p(ab…|xy…) notation.

Parameters:
  • output (list of ints.) – Conditional output as [a, b, …]
  • input (list of ints.) – The input to condition on as [x, y, …]
  • marginal (list of str.) – Optional parameter. If it is a marginal, then you can define which party or parties it belongs to.
Returns:

polynomial of sympy.physics.quantum.HermitianOperator.

Example:

For the CHSH scenario, to get p(10|01), write

P([1,0], [0,1])

To get the marginal p_A(0|1), write

P([0], [1], [‘A’])
get_all_operators()

Return all operators across all parties and measurements to supply them to the ncpol2sdpa.SdpRelaxation class.

ncpol2sdpa.bosonic_constraints(a)

Return a set of constraints that define fermionic ladder operators.

Parameters:a (list of sympy.physics.quantum.operator.Operator.) – The non-Hermitian variables.
Returns:a dict of substitutions.
ncpol2sdpa.fermionic_constraints(a)

Return a set of constraints that define fermionic ladder operators.

Parameters:a (list of sympy.physics.quantum.operator.Operator.) – The non-Hermitian variables.
Returns:a dict of substitutions.
ncpol2sdpa.pauli_constraints(X, Y, Z)

Return a set of constraints that define Pauli spin operators.

Parameters:
  • X (list of sympy.physics.quantum.operator.HermitianOperator.) – List of Pauli X operator on sites.
  • Y (list of sympy.physics.quantum.operator.HermitianOperator.) – List of Pauli Y operator on sites.
  • Z (list of sympy.physics.quantum.operator.HermitianOperator.) – List of Pauli Z operator on sites.
Returns:

tuple of substitutions and equalities.

ncpol2sdpa.get_neighbors(index, lattice_length, width=0, periodic=False)

Get the forward neighbors of a site in a lattice.

Parameters:
  • index (int.) – Linear index of operator.
  • lattice_length (int.) – The size of the 2D lattice in either dimension
  • width (int.) – Optional parameter to define width.
  • periodic (bool) – Optional parameter to indicate periodic boundary conditions.
Returns:

list of int – the neighbors in linear index.

ncpol2sdpa.get_next_neighbors(indices, lattice_length, width=0, distance=1, periodic=False)

Get the forward neighbors at a given distance of a site or set of sites in a lattice.

Parameters:
  • index (int.) – Linear index of operator.
  • lattice_length (int.) – The size of the 2D lattice in either dimension
  • width (int.) – Optional parameter to define width.
  • distance – Optional parameter to define distance.
  • periodic (bool) – Optional parameter to indicate periodic boundary conditions.
Returns:

list of int – the neighbors at given distance in linear index.

ncpol2sdpa.correlator(A, B)

Correlators between the probabilities of two parties.

Parameters:
  • A (list of list of sympy.physics.quantum.operator.HermitianOperator.) – Measurements of Alice.
  • B (list of list of sympy.physics.quantum.operator.HermitianOperator.) – Measurements of Bob.
Returns:

list of correlators.

ncpol2sdpa.generate_measurements(party, label)

Generate variables that behave like measurements.

Parameters:
  • party (list of int.) – The list of number of measurement outputs a party has.
  • label (str.) – The label to be given to the symbolic variables.
Returns:

list of list of sympy.physics.quantum.operator.HermitianOperator.

ncpol2sdpa.projective_measurement_constraints(*parties)

Return a set of constraints that define projective measurements.

Parameters:parties – Measurements of different parties.
Returns:substitutions containing idempotency, orthogonality and commutation relations.
ncpol2sdpa.maximum_violation(A_configuration, B_configuration, I, level, extra=None)

Get the maximum violation of a two-party Bell inequality.

Parameters:
  • A_configuration (list of int.) – Measurement settings of Alice.
  • B_configuration (list of int.) – Measurement settings of Bob.
  • I (list of list of int.) – The I matrix of a Bell inequality in the Collins-Gisin notation.
  • level (int.) – Level of relaxation.
Returns:

tuple of primal and dual solutions of the SDP relaxation.

ncpol2sdpa.define_objective_with_I(I, *args)

Define a polynomial using measurements and an I matrix describing a Bell inequality.

Parameters:
  • I (list of list of int.) – The I matrix of a Bell inequality in the Collins-Gisin notation.
  • args – Either the measurements of Alice and Bob or a Probability class describing their measurement operators.
Returns:

sympy.core.expr.Expr – the objective function to be solved as a minimization problem to find the maximum quantum violation. Note that the sign is flipped compared to the Bell inequality.