Skip to main content

Quantum-inspired optimization and modeling framework

Project description

Real2Quantum v0.1

Introduction

Real2Quantum is a package designed to make quantum computing more accessible for a wide range of applications. It provides implementations of various optimization problems, with version v0.1 focusing on finance use cases such as portfolio optimization and risk minimization. Future releases aim to expand into additional domains, including logistics, transportation, and energy distribution. Real2Quantum is framework-agnostic and can generate the Hamiltonian in Pennylane, Qiskit, Cirq and D-wave format.

With Real2Quantum, you can define an optimization problem and incorporate domain-specific constraints in a natural way. The framework then automatically maps the problem into its quantum formulation by constructing the corresponding Hamiltonian. Other options are also available, such as solving the problem using the QAOA algorithm on a PennyLane simulator.

Installation

  1. Clone this repository:
git clone https://github.com/rscadrien/Real2Quantum.git
  1. Install dependencies
pip install -r requirements.txt

How to use it

We will illustrate for the portfolio optimization problem (binary version). The other implemented problems (risk minimization and the multibit variations) follows the same structure. It is simply defined by the number of assets, the expected returns, the covariance matrix, and a risk–return trade-off parameter:

#Defining parameters
n = 5  # number of assets

mu = np.array([0.10, 0.12, 0.07, 0.09, 0.11])

Sigma = np.array([
    [0.10, 0.02, 0.01, 0.03, 0.02],
    [0.02, 0.08, 0.02, 0.01, 0.03],
    [0.01, 0.02, 0.09, 0.02, 0.01],
    [0.03, 0.01, 0.02, 0.07, 0.02],
    [0.02, 0.03, 0.01, 0.02, 0.06]
])
lam = 1.0
PFO_test = PortfolioOptimization_Binary(n= mu.size, mu=mu, Sigma=Sigma, lam=lam)

This creates a PFO_test object representing the optimization problem.

You can then add constraints directly through class methods. For instance, if you want to invest in exactly K assets out of the n available, you can include a budget constraint:

K=3
P = 5.0 # penalty parameter enforcing the constraint
PFO_test.add_budget_constraint(P,K)

When the problem is defined and all constraints are included, you can derive the Hamiltonian of your problem. This Hamiltonian can then be used in your preferred quantum optimization algorithms (e.g., Grover’s algorithm, QAOA, etc.).

By setting the eco option, you can choose between different frameworks: PennyLane (default), Qiskit, Cirq or D-Wave.

for Pennylane:

H = PFO_test.build_hamiltonian(eco = 'PennyLane')

for Qiskit:

H = PFO_test.build_hamiltonian(eco = 'Qiskit')

for Cirq

H = PFO_test.build_hamiltonian(eco = 'Cirq')

for D-wave

h_dwave, J_dwave, offset_dwave = PFO_test.build_hamiltonian(eco = 'DWave')

By default, the offset is included in the Hamiltonian. You can exclude it by setting offset_incl=False.

The translation of the real-world problem into a Hamiltonian is the main output of Real2Quantum. However, the framework also provides additional functionalities.

You can visualize the graph corresponding to the optimization problem as follows:

Graph = PFO_test.build_graph()

Finally, you can solve the optimization problem locally using the QAOA algorithm on a PennyLane simulator:

p=3 #Number of QAOA layer
Solution = PFO_test.solver(p)

How to contribute to Real2Quantum

Real2Quantum is flexible enough to make it easy for anyone to create a new optimization problem for quantum computing. In the current version, there are two parent classes, QUBOProblem_Binary and QUBOProblem_Multibit, which contain common methods such as building the graph, constructing the Hamiltonian, and solving the problem using the QAOA algorithm.

To define a new problem, you only need to specify how to build the objective function and how to add constraints. For example, for portfolio optimization:

class PortfolioOptimization_Binary(QUBOProblem_Binary):

    def __init__(self, n, mu, Sigma, lam=1.0):
        self.mu = mu
        self.Sigma = Sigma
        self.lam = lam

        super().__init__(n)  # calls _build_objective()

    def _build_objective(self):
        """
        Construct the mean-variance QUBO objective function.

        The objective encodes:

            x^T Σ x  -  λ μ^T x

        where:
        - The quadratic term (x^T Σ x) represents portfolio risk.
        - The linear term (μ^T x) represents expected return.
        """

        self.H_pyqubo += sum(
            self.Sigma[i, j] * self.x[i] * self.x[j]
            for i in range(self.n)
            for j in range(self.n)
        )
        self.H_pyqubo += -self.lam * sum(
            self.mu[i] * self.x[i] for i in range(self.n)
        )

    def add_budget_constraint(self, P, K):
        """
        Add a budget constraint enforcing exactly K selected assets.

        Constraint: (sum(x_i) - K)^2
        """
        self.H_pyqubo += P * (sum(self.x[i] for i in range(self.n))-K)**2
        self._compiled = False
# + others constraints

Real2Quantum aims to be an open-source project. Contributions are welcome, feel free to improve it by adding new types of optimization problems, new constraints to the existing ones, or new methods to the parent classes.

License

MIT Licence

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

real2quantum-0.1.1.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

real2quantum-0.1.1-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file real2quantum-0.1.1.tar.gz.

File metadata

  • Download URL: real2quantum-0.1.1.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for real2quantum-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3ba7509879e300454555163c6db1ed8d9d318ec9fdb0e7932cb4d1afce6ca831
MD5 cd2b468f56a8a6b0da66ad874e351d13
BLAKE2b-256 6d108df74afe0c0cea8bcef66192ec8ecadc4c92b522453987521589fce6853d

See more details on using hashes here.

File details

Details for the file real2quantum-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: real2quantum-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for real2quantum-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4db68a7ac35172f501910d9841d6ef1661ff6acdeefe6a01eba72135ae244bef
MD5 72d8622b191d2e7161398bc7b47afa58
BLAKE2b-256 bf53995a9fad628f9f20024b0e0f4adbd0d22f240d4c7f2be6cff46fe524bdcc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page