From 952f6332567e3bee160b79e9fa602a45edc51818 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Fri, 10 May 2024 14:31:03 +0000 Subject: [PATCH] style: format code with Autopep8, Black, ClangFormat, dotnet-format, Go fmt, Gofumpt, Google Java Format, isort, Ktlint, PHP CS Fixer, Prettier, RuboCop, Ruff Formatter, Rustfmt, Scalafmt, StandardJS, StandardRB, swift-format and Yapf This commit fixes the style issues introduced in 22383ad according to the output from Autopep8, Black, ClangFormat, dotnet-format, Go fmt, Gofumpt, Google Java Format, isort, Ktlint, PHP CS Fixer, Prettier, RuboCop, Ruff Formatter, Rustfmt, Scalafmt, StandardJS, StandardRB, swift-format and Yapf. Details: None --- quantum_module/quantum_optimization.py | 29 +++++++++++++++++--------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/quantum_module/quantum_optimization.py b/quantum_module/quantum_optimization.py index c5832a997..20fb7b73e 100644 --- a/quantum_module/quantum_optimization.py +++ b/quantum_module/quantum_optimization.py @@ -1,20 +1,28 @@ # Import necessary libraries import numpy as np from qiskit import QuantumCircuit, execute -from qiskit.providers.aer import AerSimulator from qiskit.optimization import QuadraticProgram +from qiskit.providers.aer import AerSimulator # Define a function to optimize a portfolio using quantum computing + + def optimize_portfolio(stocks, weights, risk_tolerance): # Define the quadratic program qp = QuadraticProgram() - qp.binary_var_list(stocks, name='x') + qp.binary_var_list(stocks, name="x") qp.minimize(linear={stocks[i]: weights[i] for i in range(len(stocks))}) - qp.subject_to(LinearConstraint(sense='LE', rhs=risk_tolerance, coeffs={stocks[i]: weights[i] for i in range(len(stocks))})) - + qp.subject_to( + LinearConstraint( + sense="LE", + rhs=risk_tolerance, + coeffs={stocks[i]: weights[i] for i in range(len(stocks))}, + ) + ) + # Convert the quadratic program to a QUBO problem qubo = qp.to_qubo() - + # Create a quantum circuit to solve the QUBO problem qc = QuantumCircuit(len(stocks)) qc.h(range(len(stocks))) @@ -22,21 +30,22 @@ def optimize_portfolio(stocks, weights, risk_tolerance): qc.x(range(len(stocks))) qc.barrier() qc.measure_all() - + # Execute the circuit on a simulator simulator = AerSimulator() job = execute(qc, simulator, shots=1000) result = job.result() counts = result.get_counts(qc) - + # Extract the optimal solution from the counts optimal_solution = max(counts, key=counts.get) - optimal_portfolio = [stocks[i] for i, x in enumerate(optimal_solution) if x == '1'] - + optimal_portfolio = [stocks[i] for i, x in enumerate(optimal_solution) if x == "1"] + return optimal_portfolio + # Example usage -stocks = ['AAPL', 'GOOG', 'MSFT'] +stocks = ["AAPL", "GOOG", "MSFT"] weights = [0.3, 0.4, 0.3] risk_tolerance = 0.5 optimal_portfolio = optimize_portfolio(stocks, weights, risk_tolerance)