tensorcircuit.applications.optimizationΒΆ

modules for QUBO problems in QAOA

tensorcircuit.applications.optimization.Ising_loss(c: Any, pauli_terms: Any, weights: List[float]) Any[source]ΒΆ

computes the loss function for the Ising model based on a given quantum circuit, a list of Pauli terms, and corresponding weights. The offset is ignored.

Parameters:
  • c – A quantum circuit object generating the state.

  • pauli_terms – A list of Pauli terms, where each term is represented as a list of 0/1 series.

  • weights – A list of weights corresponding to each Pauli term.

Return loss:

A real number representing the computed loss value.

tensorcircuit.applications.optimization.QAOA_loss(nlayers: int, pauli_terms: Any, weights: List[float], params: List[float], full_coupling: bool = False, mixer: str = 'X') Any[source]ΒΆ

computes the loss function for the Quantum Approximate Optimization Algorithm (QAOA) applied to the Ising model.

Parameters:
  • nlayers – The number of layers in the QAOA ansatz.

  • pauli_terms – A list of Pauli terms, where each term is represented as a list of 0/1 series.

  • weights – A list of weights corresponding to each Pauli term.

  • params – A list of parameter values used in the QAOA ansatz.

  • (optional) (full_coupling) – A flag indicating whether to use all-to-all coupling in mixers. Default is False.

Paran mixer (optional):

The mixer operator to use. Default is β€œX”. The other options are β€œXY” and β€œZZ”.

Returns:

The computed loss value.

tensorcircuit.applications.optimization.QUBO_QAOA(Q: Any, nlayers: int, iterations: int, vvag: bool = False, ncircuits: int = 10, init_params: List[float] | None = None, mixer: str = 'X', learning_rate: float = 0.01, callback: Callable[[List[float], float], None] | None = None, full_coupling: bool = False) Any[source]ΒΆ

Performs the QAOA on a given QUBO problem. Adam optimizer from TensorFlow is used.

Parameters:
  • Q – The n-by-n square and symmetric Q-matrix representing the QUBO problem.

  • nlayers – The number of layers (depth) in the QAOA ansatz.

  • iterations – The number of iterations to run the optimization.

  • (optional) (full_coupling) – A flag indicating whether to use vectorized variational adjoint gradient. Default is False.

  • (optional) – The number of circuits when using vectorized variational adjoint gradient. Default is 10.

  • (optional) – The initial parameters for the ansatz circuit. Default is None, which initializes the parameters randomly.

  • (optional) – The learning rate for the Adam optimizer. Default is 1e-2.

  • (optional) – A callback function that is executed during each iteration. Default is None.

  • (optional) – A flag indicating whether to use all-to-all coupling in mixers. Default is False.

Paran mixer (optional):

The mixer operator to use. Default is β€œX”. The other options are β€œXY” and β€œZZ”.

Return params:

The optimized parameters for the ansatz circuit.

tensorcircuit.applications.optimization.QUBO_QAOA_cvar(Q: Any, nlayers: int, alpha: float, nsamples: int = 1000, callback: Callable[[List[float], float], None] | None = None, expectation_based: bool = False, maxiter: int = 1000, init_params: Tuple[float] | None = None) Any[source]ΒΆ

Perform the QUBO QAOA optimization with CVaR as the loss function.

Parameters:
  • Q – The n-by-n square and symmetric Q-matrix representing the QUBO problem.

  • ansatz – The ansatz function to be used for QAOA.

  • nlayers – The number of layers (depth) in the QAOA ansatz.

  • alpha – The cut-off percentage for CVaR.

  • nsamples – The number of samples for measurements in the CVaR calculation. Default is 1000.

  • callback – A callback function to be called after each iteration. Default is None.

  • expectation_based – A flag indicating the type of CVaR ansatz (measurement-based or expectation-based). Default is False.

  • maxiter – The maximum number of iterations for the optimization. Default is 1000.

Returns:

The optimized parameters for the ansatz circuit.

tensorcircuit.applications.optimization.cvar_from_circuit(circuit: Any, nsamples: int, Q: Any, alpha: float) Any[source]ΒΆ

Directly calculate the Conditional Value at Risk (CVaR) from a circuit. The CVaR depends on a bunch of measurements.

Parameters:
  • circuit – The quantum circuit used to prepare the state.

  • nsamples – The number of samples to take for measurements.

  • Q – The Q-matrix representing the Quadratic Unconstrained Binary Optimization (QUBO) problem.

  • alpha – The cut-off percentage for CVaR.

Returns:

The calculated CVaR value.

tensorcircuit.applications.optimization.cvar_from_expectation(circuit: Any, Q: Any, alpha: float) Any[source]ΒΆ

Calculate the Conditional Value at Risk (CVaR) from the expectation values of a quantum circuit.

Parameters:
  • circuit – The quantum circuit.

  • Q – The Q-matrix representing the Quadratic Unconstrained Binary Optimization (QUBO) problem.

  • alpha – The cut-off percentage for CVaR.

Returns:

The calculated CVaR value.

tensorcircuit.applications.optimization.cvar_loss(nlayers: int, Q: Any, nsamples: int, alpha: float, expectation_based: bool, params: List[float]) Any[source]ΒΆ

Calculate the CVaR loss for a given QUBO problem using the QAOA ansatz.

Parameters:
  • nlayers – The number of layers (depth) in the QAOA ansatz.

  • Q – The Q-matrix representing the Quadratic Unconstrained Binary Optimization (QUBO) problem.

  • nsamples – The number of samples to take for measurements in the CVaR calculation.

  • alpha – The cut-off percentage for CVaR.

  • expectation_based – A flag indicating the type of CVaR ansatz (measurement-based or expectation-based).

  • params – The parameters for the QAOA ansatz circuit.

Returns:

The calculated CVaR loss.

tensorcircuit.applications.optimization.cvar_value(r: List[float], p: List[float], percent: float) Any[source]ΒΆ

Compute the Conditional Value at Risk (CVaR) based on the measurement results.

Parameters:
  • r – The observed outcomes after measurements.

  • p – Probabilities associated with each observed outcome.

  • percent – The cut-off percentage for CVaR computation.

Returns:

The calculated CVaR value.