From 098841aa337e0dc55feecc6dc2438f8da54c09d5 Mon Sep 17 00:00:00 2001 From: Takuya Yoshioka <88071178+yoshioka1128@users.noreply.github.com> Date: Thu, 1 Aug 2024 06:14:37 +0900 Subject: [PATCH] modified: fqaoa_workflow.py --- .../algorithms/fqaoa/fqaoa_workflow.py | 32 ++++------- .../algorithms/fqaoa/temp_fqaoa_v1.0.py | 54 ------------------- 2 files changed, 10 insertions(+), 76 deletions(-) delete mode 100644 src/openqaoa-core/openqaoa/algorithms/fqaoa/temp_fqaoa_v1.0.py diff --git a/src/openqaoa-core/openqaoa/algorithms/fqaoa/fqaoa_workflow.py b/src/openqaoa-core/openqaoa/algorithms/fqaoa/fqaoa_workflow.py index b1dc3553..9b9be521 100644 --- a/src/openqaoa-core/openqaoa/algorithms/fqaoa/fqaoa_workflow.py +++ b/src/openqaoa-core/openqaoa/algorithms/fqaoa/fqaoa_workflow.py @@ -12,13 +12,9 @@ QAOADescriptor, create_qaoa_variational_params, ) -from ...qaoa_components.variational_parameters.variational_baseparams import ( - QAOAVariationalBaseParams, -) from ...utilities import ( get_mixer_hamiltonian, generate_timestamp, - ground_state_hamiltonian, ) from ...optimizers.qaoa_optimizer import get_optimizer from ...backends.wrapper import SPAMTwirlingWrapper,ZNEWrapper @@ -118,16 +114,13 @@ def __init__(self, device=DeviceLocal("vectorized")): """ super().__init__(device) self.circuit_properties = CircuitProperties() - + # FQAOA default settigs self.circuit_properties.mixer_hamiltonian = "xy" self.circuit_properties.mixer_qubit_connectivity = "cyclic" - if device.device_name == 'analytical_simulator': raise ValueError("FQAOA cannot be performed on the analytical simulator.") - self.header["algorithm"] = "fqaoa" - @check_compiled def set_circuit_properties(self, mixer_hamiltonian="xy", mixer_qubit_connectivity="cyclic", **kwargs): @@ -184,7 +177,6 @@ def set_circuit_properties(self, mixer_hamiltonian="xy", mixer_qubit_connectivit else: raise ValueError("Specified argument is not supported by the circuit") self.circuit_properties = CircuitProperties(mixer_hamiltonian=mixer_hamiltonian, mixer_qubit_connectivity=mixer_qubit_connectivity, **kwargs) - # yoshioka add mixer_hamiltonian and mixer_qubit_connectivity # FQAOA fix some parameters if mixer_hamiltonian != "xy": @@ -192,8 +184,6 @@ def set_circuit_properties(self, mixer_hamiltonian="xy", mixer_qubit_connectivit if mixer_qubit_connectivity not in ['cyclic', 'chain']: raise ValueError("Invalid value for lattice. Allowed values are one-dimensional 'cyclic' and 'chain'.") - return None - def compile( self, po_problem: Optional[Tuple[QUBO, int]] = None, @@ -218,17 +208,18 @@ def compile( verbose: bool Set True to have a summary of QAOA to displayed after compilation """ - + + problem = po_problem[0] self.device.check_connection() - super().compile(problem=po_problem[0]) + super().compile(problem=problem) self.cost_hamil = Hamiltonian.classical_hamiltonian( - terms=po_problem[0].terms, coeffs=po_problem[0].weights, constant=po_problem[0].constant + terms=problem.terms, coeffs=problem.weights, constant=problem.constant ) - + self.n_qubits = self.cost_hamil.n_qubits self.n_fermions = po_problem[1] - + self.mixer_hamil = get_mixer_hamiltonian( n_qubits=self.n_qubits, mixer_type=self.circuit_properties.mixer_hamiltonian, @@ -259,7 +250,7 @@ def compile( append_state=None, init_hadamard=False, ) - + backend_dict = self.backend_properties.__dict__.copy() self.backend = get_qaoa_backend( qaoa_descriptor=self.qaoa_descriptor, @@ -290,7 +281,7 @@ def compile( order=self.error_mitigation_properties.order, steps=self.error_mitigation_properties.steps ) - + self.optimizer = get_optimizer( vqa_object=self.backend, variational_params=self.variate_params, @@ -418,7 +409,6 @@ def _serializable_dict( return serializable_dict def _get_initial_state(self): - """ Generate the quantum circuit for FQAOA initial state preparation. @@ -432,14 +422,13 @@ def _get_initial_state(self): The initial state for the FQAOA algorithm, either as a state vector (if the backend is 'vectorized') or as a quantum circuit (for other backends). """ - + fqaoa_initial = FQAOAInitial( n_qubits = self.n_qubits, n_fermions = self.n_fermions, lattice = self.circuit_properties.mixer_qubit_connectivity ) device_name = self.device.device_name - if device_name in 'vectorized': state = fqaoa_initial.get_statevector() else: @@ -457,4 +446,3 @@ def _get_initial_state(self): state = initial_circuit return state - diff --git a/src/openqaoa-core/openqaoa/algorithms/fqaoa/temp_fqaoa_v1.0.py b/src/openqaoa-core/openqaoa/algorithms/fqaoa/temp_fqaoa_v1.0.py deleted file mode 100644 index fc800659..00000000 --- a/src/openqaoa-core/openqaoa/algorithms/fqaoa/temp_fqaoa_v1.0.py +++ /dev/null @@ -1,54 +0,0 @@ -# The simplest Open FQAOA workflow -import networkx -import numpy as np - -# The portfolio optimization -def portfolio(num_assets, Budget): - from docplex.mp.model import Model - np.random.seed(1) - num_days = 15 - q = 0.01 - # Creating a random history of the forcasting for the expected return - hist_exp = (1 - 2 * np.random.rand(num_assets)).reshape(-1,1) * (np.array([np.arange(num_days) for i in range(num_assets)]) + np.random.randint(10)) + (1 - 2 * np.random.rand(num_assets, num_days)) - mu = hist_exp.mean(axis=1) - sigma = np.cov(hist_exp) - #Start the docplex model with Model("name of the model") - from openqaoa.problems.converters import FromDocplex2IsingModel - mdl = Model('Portfolio-Optimization') - x = np.array(mdl.binary_var_list(num_assets, name='asset')) - objective_function = - mu @ x + x.T @ sigma @ x - mdl.minimize(objective_function) - mdl.add_constraint(x.sum() == Budget, ctname='budget') - qubo_po = FromDocplex2IsingModel(mdl) - ising_encoding_po = qubo_po.ising_model - return ising_encoding_po, Budget - -# create a conventional FQAOA workflow -from openqaoa import FQAOA -from openqaoa.backends import create_device # for qiskit - -# parameters for fqaoa -num_assets = 10 -Budget = 5 -hopping = 1.0 -device_list = [create_device(location='local', name='qiskit.statevector_simulator'), - create_device(location='local', name='qiskit.qasm_simulator'), - create_device(location='local', name='qiskit.shot_simulator'), - create_device(location='local', name='vectorized'), - create_device(location='local', name='analytical_simulator')] -for device in device_list: - print('device: ', device.device_name) - fqaoa = FQAOA(device) - fqaoa.set_circuit_properties(p=2, init_type='ramp') -# optional: fqaoa.set_circuit_properties(p=2, init_type='ramp', mixer_qubit_connectivity='chain') -# optional: fqaoa.set_classical_optimizer(method='bfgs', jac="finite_difference") -# optional: fqaoa.set_backend_properties(n_shots = 100, seed_simulator = 1) - fqaoa.compile(portfolio(num_assets, Budget)) - fqaoa.optimize() - opt_results = fqaoa.result - cost = opt_results.optimized['cost'] - print('intermediate', opt_results.intermediate['cost']) - print('cost using FQAOA: ', cost, 'compared to 27.028662') - print('angles: ', opt_results.optimized['angles']) - print() -