### Install QuantumNPA Package Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Install the QuantumNPA package using the Julia package manager. ```julia using Pkg; Pkg.add("QuantumNPA") ``` ```julia ]add QuantumNPA ``` -------------------------------- ### Monomial Comparison Examples Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md These examples demonstrate how monomials are compared using equality and inequality operators in Julia. It shows that comparisons are defined for monomials, enabling sorting and equality checks. ```julia-repl julia> A1 == A1 true julia> A1 < A1 false julia> A1 < A2 true julia> A2 < A1*A2 true ``` -------------------------------- ### Maximize Global Guessing Probability in CHSH Setting Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/docs/src/examples.md Maximizes the global guessing probability in the CHSH setting using full statistics. This example involves creating projectors with `full=true` and defining complex constraints. ```julia # Create projectors. The keyword argument 'full=true' means that the # operator corresponding to the highest-numbered output is directly set to # the identity minus the other ones. For example, # # PA[2,1] = Id - PA[1,1] # # and # # PE[4] = Id - PE[1] - PE[2] - PE[3] # # This is meant to make working in the Collins-Gisin projection (which the # NPA code uses) more convenient. PA = projector(1, 1:2, 1:2, full=true) PB = projector(2, 1:2, 1:2, full=true) PE = projector(5, 1:4, 1, full=true) # CHSH = 2*sqrt(2) * p p = 0.9 # Expectation value of G is the probability that Eve correctly guesses # Alice's and Bob's joint outcome. G = sum(PA[a,1] * PB[b,1] * PE[2*(a-1) + b] for a in 1:2 for b in 1:2) # Ideal CHSH-violating correlations mixed with noise. N.B., the actual # constraints imposed are on the expectation values of the operators # in the array. constraints = [PA[1,1] - 0.5*Id, PA[1,2] - 0.5*Id, PB[1,1] - 0.5*Id, PB[1,2] - 0.5*Id, PA[1,1]*PB[1,1] - 0.25*(1 + p/sqrt(2))*Id, PA[1,1]*PB[1,2] - 0.25*(1 + p/sqrt(2))*Id, PA[1,2]*PB[1,1] - 0.25*(1 + p/sqrt(2))*Id, PA[1,2]*PB[1,2] - 0.25*(1 - p/sqrt(2))*Id] # This returns about 0.7467 for p = 0.9 at level 2 using the default SCS # solver. npa_max(G, 2, eq=constraints) ``` -------------------------------- ### Monomial vs. Polynomial Comparison Error Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md This example illustrates a MethodError that occurs when attempting to compare a monomial with a polynomial using the less than operator. This indicates that inequality comparisons are not defined between monomials and polynomials. ```julia-repl julia> A1 == 1*A1 true julia> A1 < 1*A1 ERROR: MethodError: no method matching isless(::Monomial, ::Polynomial) [...] ``` -------------------------------- ### Get Objective Value from JuMP Model Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Retrieves the objective value from an optimized JuMP model. This is typically called after `optimize!(model)`. ```julia-repl julia> objective_value(model) 2.8284273129779325 ``` -------------------------------- ### Load QuantumNPA Package Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Load the QuantumNPA package into the current Julia session. ```julia using QuantumNPA ``` -------------------------------- ### Constructing Operator Subtypes Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Demonstrates the creation of `Dichotomic`, `Projector`, and `Unitary` objects using their respective constructors and accessing their fields. ```julia-repl julia> x = Dichotomic(2) /2 julia> fieldnames(Dichotomic) (:input,) julia> x.input 2 julia> y = Projector(2, 3) P2|3 julia> fieldnames(Projector) (:output, :input) julia> (y.output, y.input) (2, 3) julia> u = Unitary(3, true) U*3 julia> fieldnames(Unitary) (:index, :conj) julia> (u.index, u.conj) (3, true) ``` -------------------------------- ### Polynomial Constructors Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Shows various ways to construct Polynomial objects, including from scratch, numbers, monomials, and existing polynomials. ```julia-repl julia> Polynomial() 0 julia> Polynomial(3) 3 Id julia> Polynomial(A1) A1 julia> Polynomial(3, A1) 3 A1 julia> Polynomial(P) 3 Id ``` -------------------------------- ### Operator Printing and Identity Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Shows how different operators can be printed the same way due to party labeling conventions and demonstrates the identity operator. ```julia-repl julia> x = unitary(1, 3) UA3 julia> y = dichotomic(547, 3) UA3 julia> y*y Id julia> x*x UA3 UA3 julia> conj(x)*x Id julia> x*y UA3 UA3 julia> conj(x)*y UA*3 UA3 ``` -------------------------------- ### Checking and Promoting Object Types to Polynomials Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Demonstrates how to check the type of an object and explicitly promote it to a Polynomial type. Note that promoting an object that is already a polynomial returns the same object, implying modifications to the returned object will affect the original. ```julia-repl julia> P = projector(1, 1, 1) PA1|1 julia> typeof(P) Monomial julia> Q = 1*P PA1|1 julia> typeof(Q) Polynomial julia> S A1 B1 + A1 B2 + A2 B1 - A2 B2 julia> typeof(S) Polynomial julia> x = Polynomial(1) Id julia> y = Polynomial(Id) Id julia> z = Polynomial(S) A1 B1 + A1 B2 + A2 B1 - A2 B2 julia> typeof.([x, y, z]) 3-element Array{DataType,1}: Polynomial Polynomial Polynomial ``` -------------------------------- ### Set Default Solver to Mosek Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/docs/src/examples.md Shows how to change the default solver for QuantumNPA to Mosek using `set_solver!` function, avoiding the need to specify it in each call. ```julia-repl julia> set_solver!(Mosek.Optimizer) ``` -------------------------------- ### Solve Problem Using Mosek Solver Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/docs/src/examples.md Demonstrates how to specify a different solver, Mosek, for the `npa_max` function. Requires importing MosekTools. ```julia-repl julia> using MosekTools julia> npa_max(S, 2, solver=Mosek.Optimizer) 2.82842711211242 ``` -------------------------------- ### Construct and Solve JuMP Model with SCS Optimizer Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/docs/src/examples.md Constructs a JuMP model from an NPA specification and solves it using the SCS optimizer. The output from the solver is displayed. ```julia-repl julia> model = npa2jump(S, "1 + A B", solver=SCS.Optimizer) A JuMP Model Minimization problem with: Variables: 45 Objective function type: AffExpr `AffExpr`-in-`MathOptInterface.EqualTo{Float64}`: 16 constraints `Vector{VariableRef}`-in-`MathOptInterface.PositiveSemidefiniteConeTriangle`: 1 constraint Model mode: AUTOMATIC CachingOptimizer state: EMPTY_OPTIMIZER Solver name: SCS julia> optimize!(model) ------------------------------------------------------------------ SCS v3.2.0 - Splitting Conic Solver (c) Brendan O'Donoghue, Stanford University, 2012 ------------------------------------------------------------------ problem: variables n: 45, constraints m: 61 cones: z: primal zero / dual free vars: 16 s: psd vars: 45, ssize: 1 settings: eps_abs: 1.0e-04, eps_rel: 1.0e-04, eps_infeas: 1.0e-07 alpha: 1.50, scale: 1.00e-01, adaptive_scale: 1 max_iters: 100000, normalize: 1, rho_x: 1.00e-06 acceleration_lookback: 10, acceleration_interval: 10 lin-sys: sparse-direct nnz(A): 81, nnz(P): 0 ------------------------------------------------------------------ iter | pri res | dua res | gap | obj | scale | time (s) ------------------------------------------------------------------ 0| 2.22e+01 1.00e+00 2.00e+02 -9.98e+01 1.00e-01 1.40e-04 75| 1.51e-05 5.94e-08 5.35e-08 2.83e+00 1.00e-01 3.10e-03 ------------------------------------------------------------------ status: solved timings: total: 3.48e-03s = setup: 3.66e-04s + solve: 3.11e-03s lin-sys: 2.47e-04s, cones: 2.58e-03s, accel: 1.57e-05s ------------------------------------------------------------------ objective = 2.828427 ------------------------------------------------------------------ julia> objective_value(model) 2.8284273129779325 ``` -------------------------------- ### Maximize with Equality and Inequality Constraints Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/docs/src/examples.md Demonstrates maximizing an operator subject to equality and inequality constraints on expectation values. Uses `eq` and `ge` keyword arguments. ```julia-repl julia> npa_max(A1, 2, eq=[A1*(B1 + B2) - 1.4*Id, A2*(B1 - B2) - 1.4*Id]) 0.19800616634180992 ``` -------------------------------- ### Basic Operator Arithmetic and Conjugation Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Illustrates arithmetic operations and conjugation for various operator types including projectors, unitaries, generics, and the identity operator. ```julia-repl julia> Id Id julia> projector(1, 2, 3) PA2|3 julia> PA = projector(1, 1:2, 1:2); julia> PB = projector(2, 1:2, 1:2); julia> PA[1,1] PA1|1 julia> PA[1,1]*PB[1,1] PA1|1 PB1|1 julia> PB[1,1]*PA[1,1] PA1|1 PB1|1 julia> PA[1,1]*PA[2,1] 0 julia> PA[1,1]*PA[1,1] PA1|1 julia> UA = unitary(1, 1:3); julia> V = UA[1]*conj(UA[2])*UA[3] UA1 UA*2 UA3 julia> P = PA[1,1] + V PA1|1 + UA1 UA*2 UA3 julia> conj(P) PA1|1 + UA*3 UA2 UA*1 julia> P*P PA1|1 + PA1|1 UA1 UA*2 UA3 + UA1 UA*2 UA3 PA1|1 + UA1 UA*2 UA3 UA1 UA*2 UA3 julia> conj(P)*P Id + PA1|1 + PA1|1 UA1 UA*2 UA3 + UA*3 UA2 UA*1 PA1|1 julia> Q = Id + V*PA[1,1] Id + UA1 UA*2 UA3 PA1|1 julia> Q*Q Id + 2 UA1 UA*2 UA3 PA1|1 + UA1 UA*2 UA3 PA1|1 UA1 UA*2 UA3 PA1|1 julia> conj(Q)*Q Id + PA1|1 + PA1|1 UA*3 UA2 UA*1 + UA1 UA*2 UA3 PA1|1 julia> ZE = generic(5, 1:2); julia> R = PA[1,1]*PB[2,2]*ZE[1]*ZE[2] PA1|1 PB2|2 ZE1 ZE2 julia> conj(R) PA1|1 PB2|2 ZE*2 ZE*1 julia> FA = fourier(1, 9, 1, 5) A9^1 julia> FA^0 Id julia> FA^3 A9^3 julia> conj(FA^3) A9^2 julia> FA^5 Id julia> FA^6 A9^1 julia> conj(FA^3)*FA^3 Id julia> FA*FA A9^2 julia> conj((FA*FA)^4) A9^2 julia> A1, A2 = dichotomic(1, 1:2); julia> B1, B2 = dichotomic(2, 1:2); julia> S = A1*(B1 + B2) + A2*(B1 - B2) A1 B1 + A1 B2 + A2 B1 - A2 B2 julia> S^2 4 Id - A1 A2 B1 B2 + A1 A2 B2 B1 + A2 A1 B1 B2 - A2 A1 B2 B1 ``` -------------------------------- ### Finding All Operators in a Problem Definition Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Demonstrates the use of the `operators()` function to find all individual level-one operators present in an objective polynomial and a list of constraint polynomials. ```julia A1, A2 = dichotomic(1, 1:2) B1, B2 = dichotomic(2, 1:2) E1 = dichotomic(5, 1) objective = (1 + A1*E1)/2 constraints = [A1, A2, B1, B2, A1*B1 - 0.7*Id, A1*B2 - 0.7*Id, A2*B1 - 0.7*Id, A2*B2 + 0.7*Id] ``` ```julia-repl julia> operators(objective, constraints) Set{Monomial} with 5 elements: A2 A1 B1 B2 E1 ``` -------------------------------- ### Polynomials with Matrix Coefficients Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Demonstrates the creation and multiplication of polynomials with matrix coefficients, which are used to represent the NPA moment matrix. ```julia-repl julia> P = [1 0; 0 1] * Id + [0 1; 1 0] * A1 [1 0; 0 1] Id + [0 1; 1 0] A1 julia> Q = [1 2; 3 4] * Id + [5 6; 7 8] * B1 [1 2; 3 4] Id + [5 6; 7 8] B1 julia> P*Q [1 2; 3 4] Id + [3 4; 1 2] A1 + [5 6; 7 8] B1 + [7 8; 5 6] A1 B1 ``` -------------------------------- ### Grouping Operators by Party Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Illustrates how to use the `operators()` function with the `by_party=true` keyword argument to group level-one operators by their associated party, returning a dictionary. ```julia-repl julia> operators(objective, constraints, by_party=true) Dict{Integer,Set{Monomial}} with 3 entries: 2 => Set(Monomial[B1, B2]) 5 => Set(Monomial[E1]) 1 => Set(Monomial[A2, A1]) ``` -------------------------------- ### Displaying Polynomial Terms Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Shows how polynomial terms and their coefficients are stored and displayed in the REPL. ```julia-repl julia> P = 3*Id + 2*A1*A2 3 Id + 2 A1 A2 julia> P.terms Dict{Monomial, Number} with 2 entries: Id => 3 A1 A2 => 2 ``` -------------------------------- ### Joining Operator Products Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Demonstrates the `join_ops` function, which combines two lists of operators by multiplying their last elements and concatenating the results with the remaining operators. ```julia-repl julia> p = [u, x] 2-element Vector{Operator}: U*3 /2 julia> q = [x, y] 2-element Vector{Operator}: /2 P2|3 julia> join_ops(p, q) (1, Operator[U*3, P2|3]) ``` -------------------------------- ### Iterating Over Polynomial Coefficients and Monomials Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Illustrates that polynomials can be iterated over, yielding pairs of coefficients and their corresponding monomials. The default iteration order is not guaranteed to be sorted. ```julia-repl julia> collect(S) 4-element Vector{Any}: Pair{Number,Monomial}(-1, A2 B2) Pair{Number,Monomial}(1, A1 B2) Pair{Number,Monomial}(1, A2 B1) Pair{Number,Monomial}(1, A1 B1) julia> for (c, m) in S @printf "%s => %2d\n" m c end A2 B1 => 1 A1 B2 => 1 A2 B2 => -1 A1 B1 => 1 ``` -------------------------------- ### Construct Monomial with Single Operator Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Shows how to create a Monomial object containing a single operator by providing a party vector and the operator. This is a recommended way to build monomials. ```julia-repl julia> M = Monomial([1], Dichotomic(2)) A2 julia> M.word 1-element Vector{Tuple{Vector{Int64}, Vector{Operator}}}: ([1], [/2]) ``` -------------------------------- ### Projector Function Variations Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Illustrates different ways to call the projector() function, including specifying input/output ranges and the 'full' parameter. ```julia-repl julia> projector(1, 1:2, 1:2) 2×2 Array{Monomial,2}: PA1|1 PA1|2 PA2|1 PA2|2 julia> projector(1, 1:3, 1:2, full=true) 3×2 Array{Any,2}: PA1|1 PA1|2 PA2|1 PA2|2 Id - PA1|1 - PA2|1 Id - PA1|2 - PA2|2 julia> generic(1, 1:3) 3-element Array{Monomial,1}: ZA1 ZA2 ZA3 ``` -------------------------------- ### Creating Invalid Polynomials Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Illustrates the internal Polynomial constructor and its potential to create 'invalid' polynomials. ```julia-repl julia> Q = Polynomial(Dict(A1*A2 => 0)) 0 A1 A2 julia> Q == 0 false ``` -------------------------------- ### Multiplying Operators of Different Types Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Illustrates the result of multiplying operators of different types using the generic multiplication rule, resulting in a coefficient and a list of the operators. ```julia-repl julia> x*y (1, Operator[/2, P2|3]) ``` -------------------------------- ### Iterating Over Sorted Monomials in a Polynomial Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Shows how to iterate over the coefficients and monomials of a polynomial in lexicographical order by first applying the `sort()` function to the polynomial. ```julia-repl julia> for (c, m) in sort(S) @printf "%s => %2d\n" m c end A1 B1 => 1 A1 B2 => 1 A2 B1 => 1 A2 B2 => -1 ``` -------------------------------- ### Maximize with Mixed Equality and Inequality Constraints Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/docs/src/examples.md Maximizes an operator subject to both equality (`eq`) and inequality (`ge`) constraints. Demonstrates handling of mixed constraint types. ```julia-repl julia> npa_max(A1 + A2, 1, eq=[A1 - A2], ge=[1 - A1 - 2*A2]) 0.666642228695571 ``` -------------------------------- ### npa2sdp(expr, level; eq=[], ge=[]) Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/docs/src/npa.md Converts an expression to an SDP (Semidefinite Programming) form for a given level, with optional equality and inequality constraints. This function is used to formulate optimization problems for NPA. ```APIDOC ## npa2sdp(expr, level; eq=[], ge=[]) ### Description Converts an expression to an SDP (Semidefinite Programming) form for a given level, with optional equality and inequality constraints. This function is used to formulate optimization problems for NPA. ### Method Not Applicable (Julia function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```julia expr = "some_expression" level = 3 equalities = [Eq1, Eq2] inequalities = [Ineq1] npa2sdp(expr, level, eq=equalities, ge=inequalities) ``` ### Response #### Success Response - **sdp_form** (Any) - The expression converted into SDP form. #### Response Example ```julia # Example of a returned SDP form (structure depends on SDP solver) "SDP representation of the expression" ``` ``` -------------------------------- ### Dichotomic Operator Commutation Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Illustrates the commutation relations between different dichotomic operators. Observe how operator order affects the resulting expression. ```julia-repl julia> A1, A2 = dichotomic(1, 1:2); julia> B1, B2 = dichotomic(2, 1:2); julia> A_C1 = dichotomic([1,3], 1); julia> A1*B1 A1 B1 julia> B1*A1 A1 B1 julia> A1*A_C1 A1 A_C1 julia> A_C1*A1 A_C1 A1 julia> A1*B1*A_C1 A1 B1 A_C1 julia> A1*B1*A_C1*A2*B2 A1 B1 B2 A_C1 A2 julia> A1*B1*A_C1*A1*B1 A1 A_C1 A1 julia> X = A1*B1 A1 B1 julia> Y = A_C1*X B1 A_C1 A1 julia> A_C1*Y A1 B1 ``` -------------------------------- ### Macro for Dichotomic Variable Creation Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Demonstrates the use of the `@dichotomic` macro to efficiently create multiple dichotomic variables with standard naming conventions. ```julia @dichotomic A1 A2 B1 B2 C[1:3] ``` ```julia A1 = dichotomic(1, 1) A2 = dichotomic(1, 2) B1 = dichotomic(2, 1) B2 = dichotomic(2, 2) C = dichotomic(3, 1:3) ``` -------------------------------- ### Extracting Monomials from a Polynomial Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Demonstrates how to retrieve all unique monomials present in a polynomial using the `monomials()` function. The output is a set of the monomials. ```julia-repl julia> monomials(S) Set{Monomial} with 4 elements: Keys: A1 B2 A1 B1 A2 B1 A2 B2 ``` -------------------------------- ### Conjugating a Dichotomic Operator Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Illustrates the behavior of the generic `conj()` function when applied to a `Dichotomic` operator, showing that it remains unchanged. ```julia-repl julia> cx = conj(x) /2 julia> cx === x true ``` -------------------------------- ### npa_moment(source, level) Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/docs/src/npa.md Generates a moment matrix from a source and a specified level. This is an alternative interface for moment matrix generation. ```APIDOC ## npa_moment(source, level) ### Description Generates a moment matrix from a source and a specified level. This is an alternative interface for moment matrix generation. ### Method Not Applicable (Julia function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```julia source = "some_quantum_state" level = 2 npa_moment(source, level) ``` ### Response #### Success Response - **moment_matrix** (Matrix) - The generated moment matrix. #### Response Example ```julia # Example of a returned moment matrix structure [1.0 0.2; 0.2 1.0] ``` ``` -------------------------------- ### Maximise with Equality and Inequality Constraints Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Maximizes subject to an equality constraint = and an inequality constraint. Uses `eq` and `ge` keyword arguments. ```julia-repl julia> npa_max(A1 + A2, 1, eq=[A1 - A2], ge=[Id - A1 - 2*A2]) 0.666642228695571 ``` -------------------------------- ### Operators in Arrays and Vector Expressions Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Illustrates how dichotomic operators can be stored in Julia arrays and used in vector and matrix expressions, including Kronecker products. ```julia-repl julia> A = dichotomic(1, 1:2) 2-element Vector{Monomial}: A1 A2 julia> B = dichotomic(2, 1:2) 2-element Vector{Monomial}: B1 B2 julia> M = [1 1; 1 -1] 2×2 Matrix{Int64}: 1 1 1 -1 julia> A'*M*B A1 B1 + A1 B2 + A2 B1 - A2 B2 julia> kron(A, B) 4-element Vector{Union{Monomial, Polynomial}}: A1 B1 A1 B2 A2 B1 A2 B2 julia> [1,1,1,-1]'*kron(A, B) A1 B1 + A1 B2 + A2 B1 - A2 B2 julia> V = [Id; A; B; kron(A, B)]' 1×9 adjoint(::Vector{Union{Monomial, Polynomial}}) with eltype Union{Monomial, Polynomial}: Id A1 A2 B1 B2 A1 B1 A1 B2 A2 B1 A2 B2 julia> V'*V 9×9 Matrix{Monomial}: Id A1 A2 B1 B2 … A2 B1 A2 B2 A1 Id A1 A2 A1 B1 A1 B2 A1 A2 B1 A1 A2 B2 A2 A2 A1 Id A2 B1 A2 B2 B1 B2 B1 A1 B1 A2 B1 Id B1 B2 A2 A2 B1 B2 B2 A1 B2 A2 B2 B2 B1 Id A2 B2 B1 A2 A1 B1 B1 A1 A2 B1 A1 A1 B1 B2 … A1 A2 A1 A2 B1 B2 A1 B2 B2 A1 A2 B2 A1 B2 B1 A1 A1 A2 B2 B1 A1 A2 A2 B1 A2 A1 B1 B1 A2 A2 B1 B2 Id B1 B2 A2 B2 A2 A1 B2 B2 A2 B2 B1 A2 B2 B1 Id ``` -------------------------------- ### Maximize CHSH Inequality Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/docs/src/index.md Use this snippet to define dichotomic observables and maximize the CHSH inequality using the npa_max function. Requires importing the QuantumNPA package. ```julia-repl julia> using QuantumNPA julia> @dichotomic A1 A2 B1 B2; julia> S = A1*(B1 + B2) + A2*(B1 - B2); julia> npa_max(S, 2) 2.828227712681755 ``` -------------------------------- ### Applying Specialized Conjugation to a Unitary Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Shows how calling `conj()` on a `Unitary` object toggles its `conj` field, creating a new `Unitary` object with the updated state. ```julia-repl julia> u.conj true julia> v = conj(u) U3 julia> v.conj false ``` -------------------------------- ### Inspect Monomial Word Field Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Demonstrates how to create and inspect the 'word' field of a Monomial, showing its internal structure of party vectors and associated operators. ```julia-repl julia> (A1, A2) = dichotomic(1, 1:2); julia> PB11 = projector(2, 1, 1); julia> (UE1, UE2) = unitary(5, 1:2); julia> M = A1*A2*PB11*UE1*UE2 A1 A2 PB1|1 UE1 UE2 julia> M.word 3-element Vector{Tuple{Vector{Int64}, Vector{Operator}}}: ([1], [/1, /2]) ([2], [P1|1]) ([5], [U1, U2]) ``` -------------------------------- ### Operator Multiplication with Projectors Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Shows the multiplication of a dichotomic operator with a projector and subtraction involving projectors and the identity operator. ```julia-repl julia> dichotomic(1, 1) * projector(1, 1, 1) A1 PA1|1 julia> dichotomic(1, 1) - (2*projector(1, 1, 1) - Id) Id + A1 - 2 PA1|1 ``` -------------------------------- ### Specialized Conjugation for Unitary Operators Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Demonstrates the specialized `conj()` implementation for `Unitary` objects, which toggles the `conj` field. This is generated by a macro. ```julia Base.conj(u::Unitary) = Unitary(u.index, !u.conj) ``` -------------------------------- ### Modifying Polynomials and Ensuring Independent Copies Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Illustrates that modifying a polynomial obtained via promotion (e.g., `z = Polynomial(S)`) directly alters the original polynomial (`S`). To avoid this, use the `copy()` function to create an independent mutable copy of a polynomial. ```julia-repl julia> z === S true julia> z[A1] = 7 7 julia> S 7 A1 + A1 B1 + A1 B2 + A2 B1 - A2 B2 julia> S = A1*(B1 + B2) + A2*(B1 - B2) A1 B1 + A1 B2 + A2 B1 - A2 B2 julia> z = copy(S) A1 B1 + A1 B2 + A2 B1 - A2 B2 julia> z === S false julia> z[A1] = 7 7 julia> z 7 A1 + A1 B1 + A1 B2 + A2 B1 - A2 B2 julia> S A1 B1 + A1 B2 + A2 B1 - A2 B2 ``` -------------------------------- ### Display NPA Moment Matrix Indices Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md This code iterates through the computed indices of the NPA moment matrix and prints each operator along with its corresponding list of index pairs. The output is sorted by the operator. ```julia-repl julia> for (m, l) in sort!(collect(indices), by=first) @printf "%11s => %s\n" m l end Id => [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9), (10, 10), (11, 11), (12, 12), (13, 13)] A1 => [(1, 2), (3, 7), (4, 8), (5, 9)] A2 => [(1, 3), (2, 6), (4, 10), (5, 11)] B1 => [(1, 4), (2, 8), (3, 10), (5, 13)] B2 => [(1, 5), (2, 9), (3, 11), (4, 12)] A1 A2 => [(1, 6), (1, 7), (2, 3), (8, 10), (9, 11)] A1 B1 => [(1, 8), (2, 4), (7, 10), (9, 13)] A1 B2 => [(1, 9), (2, 5), (7, 11), (8, 12)] A2 B1 => [(1, 10), (3, 4), (6, 8), (11, 13)] A2 B2 => [(1, 11), (3, 5), (6, 9), (10, 12)] B1 B2 => [(1, 12), (1, 13), (4, 5), (8, 9), (10, 11)] A1 A2 A1 => [(2, 7)] A2 A1 A2 => [(3, 6)] A1 A2 B1 => [(2, 10), (3, 8), (4, 6), (4, 7)] A1 A2 B2 => [(2, 11), (3, 9), (5, 6), (5, 7)] A1 B1 B2 => [(2, 12), (2, 13), (4, 9), (5, 8)] A2 B1 B2 => [(3, 12), (3, 13), (4, 11), (5, 10)] B1 B2 B1 => [(4, 13)] B2 B1 B2 => [(5, 12)] A1 A2 A1 A2 => [(6, 7)] A1 A2 A1 B1 => [(7, 8)] A1 A2 A1 B2 => [(7, 9)] A2 A1 A2 B1 => [(6, 10)] A2 A1 A2 B2 => [(6, 11)] A1 A2 B1 B2 => [(6, 13), (7, 12), (8, 11)] A1 A2 B2 B1 => [(6, 12), (7, 13), (9, 10)] A1 B1 B2 B1 => [(8, 13)] A1 B2 B1 B2 => [(9, 12)] A2 B1 B2 B1 => [(10, 13)] A2 B2 B1 B2 => [(11, 12)] B1 B2 B1 B2 => [(12, 13)] ``` -------------------------------- ### Maximize with Inequality Constraints Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/docs/src/examples.md Maximizes an operator subject to upper bounds on expectation values using the `ge` keyword argument. Constraints are defined as `Id - operator`. ```julia-repl julia> npa_max(A1 + A2, 1, ge=[Id - A1 - 2*A2, Id - 2*A1 - A2]) 0.6666666597867417 ``` -------------------------------- ### Accessing and Modifying Polynomial Coefficients Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Shows how to access and modify the coefficient of a specific monomial within a polynomial using bracket notation `[]`. If a monomial is not explicitly present, its coefficient is treated as zero. ```julia-repl julia> S[A1*B1] 1 julia> S[A1] 0 ``` -------------------------------- ### Maximize CH74 Form of CHSH using Projectors Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/docs/src/examples.md Maximizes the CH74 form of the CHSH inequality using projector operators instead of dichotomic variables. Requires defining projectors for parties A and B. ```julia-repl julia> PA11, PA12 = projector(1,1,1:2); julia> PB11, PB12 = projector(2,1,1:2); julia> npa_max(-PA11 - PB11 + PA11*(PB11 + PB12) + PA12*(PB11 - PB12), 1) 0.20701116471401693 ``` -------------------------------- ### Modifying Polynomial Terms Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Demonstrates how setting a term's coefficient to zero removes it from the polynomial. ```julia-repl julia> P 3 Id + 2 A1 A2 julia> P[A1*A2] = 0 0 julia> P 3 Id julia> P.terms Dict{Monomial, Number} with 1 entry: Id => 3 ``` -------------------------------- ### Maximize Svetlichny Inequality Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/docs/src/examples.md Maximizes the Svetlichny inequality for three parties (A, B, C) at a specific hierarchy level. Requires defining multi-outcome dichotomic variables. ```julia-repl julia> @dichotomic A[1:2] B[1:2] C[1:2]; julia> E(x,y,z) = A[x]*B[y]*C[z] E (generic function with 1 method) julia> S = -E(1,1,1) + E(1,1,2) + E(1,2,1) + E(1,2,2) + E(2,1,1) + E(2,1,2) + E(2,2,1) - E(2,2,2) -A1 B1 C1 + A1 B1 C2 + A1 B2 C1 + A1 B2 C2 + A2 B1 C1 + A2 B1 C2 + A2 B2 C1 - A2 B2 C2 julia> npa_max(S, "1 + A B + A C + B C") 5.656854315137034 ``` -------------------------------- ### Maximize CGLMP Inequality with d=3 Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/docs/src/examples.md Maximizes the CGLMP inequality for a dimension d=3 at a specific hierarchy level. Uses a predefined `cglmp()` function to construct the operator. ```julia-repl julia> npa_max(cglmp(3), "1 + A B") 2.914945976226541 ``` -------------------------------- ### npa_moment(operators::Vector) Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/docs/src/npa.md Generates a moment matrix from a vector of operators. This function is a primary interface for NPA calculations. ```APIDOC ## npa_moment(operators::Vector) ### Description Generates a moment matrix from a vector of operators. This function is a primary interface for NPA calculations. ### Method Not Applicable (Julia function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```julia operators = [Op1, Op2, Op3] npa_moment(operators) ``` ### Response #### Success Response - **moment_matrix** (Matrix) - The generated moment matrix. #### Response Example ```julia # Example of a returned moment matrix structure [1.0 0.5; 0.5 1.0] ``` ``` -------------------------------- ### Generic Conjugation for Operators Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Shows the default implementation of `conj()` for the abstract `Operator` type, which returns the operator unchanged. This is suitable for Hermitian operators. ```julia Base.conj(o::Operator) = o ``` -------------------------------- ### Monomial Multiplication with Numbers Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Defines multiplication of a number by a monomial and vice versa, resulting in a Polynomial. ```julia Base.:*(x::Number, y::Monomial) = Polynomial(x, y) Base.:*(x::Monomial, y::Number) = Polynomial(y, x) ``` -------------------------------- ### Calculate NPA Moment Matrix Operators Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md This snippet computes the operators and their indices for the upper triangular part of the NPA moment matrix. It treats monomials and their conjugates as the same for indexing. ```julia A1, A2 = dichotomic(1, 1:2) B1, B2 = dichotomic(2, 1:2) ops1 = [Id, A1, A2, B1, B2] ops2 = sort(Set(O1*O2 for O1 in ops1 for O2 in ops1)) indices = Dict() indexed_ops = collect(enumerate(ops2)) for (i, x) in indexed_ops for (j, y) in indexed_ops[i:end] m = conj(x)*y m = min(m, conj(m)) if haskey(indices, m) push!(indices[m], (i, j)) else indices[m] = [(i, j)] end end end ``` -------------------------------- ### Generic Operator Multiplication Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Defines the default behavior for multiplying two `Operator` types, which is to return a coefficient of 1 and a list containing both operators. ```julia Base.:*(x::Operator, y::Operator) = (1, [x, y]) ``` -------------------------------- ### Maximize CHSH at Hierarchy Level 2 Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/docs/src/examples.md Maximizes the CHSH inequality at level 2 of the NPA hierarchy. Requires defining dichotomic variables A1, A2, B1, B2. ```julia-repl julia> @dichotomic A1 A2 B1 B2; julia> S = A1*(B1 + B2) + A2*(B1 - B2) A1 B1 + A1 B2 + A2 B1 - A2 B2 julia> npa_max(S, 2) 2.828227712681755 ``` -------------------------------- ### Specialized Multiplication for Dichotomic Operators Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/README.md Implements the multiplication rule for two `Dichotomic` operators. If their `input` fields match, the result is the identity (represented by an empty list); otherwise, they concatenate. ```julia function Base.:*(x::Dichotomic, y::Dichotomic) return (x.input == y.input) ? (1, []) : (1, [x, y]) end ``` -------------------------------- ### Maximize Modified CHSH Inequality Source: https://github.com/ewoodhead/quantumnpa.jl/blob/main/docs/src/examples.md Maximizes a modified CHSH inequality with additional terms at a specific hierarchy level. Supports custom operator definitions. ```julia-repl julia> npa_max(0.3 * A1 + 0.6 * A1*(B1 + B2) + A2*(B1 - B2), "1 + A B + A^2 B") 2.3584761820283977 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.