Skip to content

Commit

Permalink
Merge pull request #179 from entropicalabs/refactor_qr
Browse files Browse the repository at this point in the history
Refactor qr
  • Loading branch information
vishal-ph authored Feb 22, 2023
2 parents 62f7fa4 + 26f0c9d commit 7f732a6
Show file tree
Hide file tree
Showing 32 changed files with 3,599 additions and 2,740 deletions.
479 changes: 225 additions & 254 deletions examples/01_workflows_example.ipynb

Large diffs are not rendered by default.

858 changes: 696 additions & 162 deletions examples/03_qaoa_on_qpus.ipynb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/04_qaoa_variational_parameters.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
"name": "oq_reviews"
},
"language_info": {
"codemirror_mode": {
Expand Down
5 changes: 5 additions & 0 deletions src/openqaoa-azure/backends/devices.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import List

from azure.quantum.qiskit import AzureQuantumProvider
from openqaoa.backends.devices_core import DeviceBase

Expand Down Expand Up @@ -96,3 +98,6 @@ def _check_provider_connection(self) -> bool:
)
)
return False

def connectivity(self) -> List[List[int]]:
return self.backend_device.configuration().coupling_map
5 changes: 4 additions & 1 deletion src/openqaoa-braket/backends/devices.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np

from typing import List
from boto3.session import Session
from botocore.exceptions import NoRegionError
from braket.aws import AwsDevice
Expand Down Expand Up @@ -147,3 +147,6 @@ def _check_provider_connection(self) -> bool:
)
)
return False

def connectivity(self) -> List[List[int]]:
return self.backend_device.topology_graph.edges
75 changes: 42 additions & 33 deletions src/openqaoa-braket/backends/qaoa_braket_qpu.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from typing import Optional, List
import warnings

from braket.circuits import Circuit
from braket.circuits.gates import H
Expand Down Expand Up @@ -47,32 +48,33 @@ class QAOAAWSQPUBackend(
used. This is False by default. Not all providers provide this feature.
"""

def __init__(
self,
qaoa_descriptor: QAOADescriptor,
device: DeviceAWS,
n_shots: int,
prepend_state: Optional[Circuit],
append_state: Optional[Circuit],
init_hadamard: bool,
cvar_alpha: float,
qubit_layout: List[int] = [],
disable_qubit_rewiring: bool = False,
):

QAOABaseBackendShotBased.__init__(
self,
qaoa_descriptor,
n_shots,
prepend_state,
append_state,
init_hadamard,
cvar_alpha,
)
def __init__(self,
qaoa_descriptor: QAOADescriptor,
device: DeviceAWS,
n_shots: int,
prepend_state: Optional[Circuit],
append_state: Optional[Circuit],
init_hadamard: bool,
cvar_alpha: float,
disable_qubit_rewiring: bool = False,
initial_qubit_mapping: Optional[List[int]] = None):

QAOABaseBackendShotBased.__init__(self,
qaoa_descriptor,
n_shots,
prepend_state,
append_state,
init_hadamard,
cvar_alpha,
)
QAOABaseBackendCloud.__init__(self, device)

self.qureg = self.qaoa_descriptor.qureg
self.qubit_layout = self.qureg if qubit_layout == [] else qubit_layout
self.qureg = list(range(self.n_qubits))
self.problem_reg = self.qureg[0:self.problem_qubits]
if self.initial_qubit_mapping is None:
self.initial_qubit_mapping = initial_qubit_mapping if initial_qubit_mapping is not None else list(range(self.n_qubits))
else:
warnings.warn("Ignoring the initial_qubit_mapping since the routing algorithm chose one")
self.disable_qubit_rewiring = disable_qubit_rewiring

if self.prepend_state:
Expand Down Expand Up @@ -145,15 +147,17 @@ def parametric_qaoa_circuit(self) -> Circuit:

# Initial state is all |+>
if self.init_hadamard:
for each_qubit in self.qubit_layout:
for each_qubit in self.problem_reg:
parametric_circuit += H.h(each_qubit)

self.braket_parameter_list = []
for each_gate in self.abstract_circuit:
angle_param = FreeParameter(str(each_gate.pauli_label))
self.braket_parameter_list.append(angle_param)
each_gate.rotation_angle = angle_param
decomposition = each_gate.decomposition("standard")
#if gate is of type mixer or cost gate, assign parameter to it
if each_gate.gate_label.type.value in ['mixer','cost']:
angle_param = FreeParameter(each_gate.gate_label.__repr__())
self.braket_parameter_list.append(angle_param)
each_gate.angle_value = angle_param
decomposition = each_gate.decomposition('standard')
# using the list above, construct the circuit
for each_tuple in decomposition:
gate = each_tuple[0]()
Expand All @@ -164,6 +168,7 @@ def parametric_qaoa_circuit(self) -> Circuit:
if self.append_state:
parametric_circuit += self.append_state

#TODO: needs to be fixed --> measurement operations on problem qubits
parametric_circuit += Probability.probability()

return parametric_circuit
Expand Down Expand Up @@ -233,10 +238,14 @@ def get_counts(self, params: QAOAVariationalBaseParams, n_shots=None) -> dict:
"An Error Occurred with the Task(s) sent to AWS."
)

# Expose counts
self.measurement_outcomes = counts
return counts

final_counts = counts
# if self.final_mapping is not None:
# final_counts = permute_counts_dictionary(final_counts,
# self.final_mapping)
# # Expose counts
self.measurement_outcomes = final_counts
return final_counts

def log_with_backend(self, metric_name: str, value, iteration_number) -> None:

"""
Expand Down
29 changes: 25 additions & 4 deletions src/openqaoa-core/algorithms/qaoa/qaoa_workflow.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import List, Callable, Optional
import requests
from .qaoa_result import QAOAResult
from ..workflow_properties import CircuitProperties
from ..baseworkflow import Workflow
from ...backends.devices_core import DeviceLocal
from ...backends.devices_core import DeviceLocal, DeviceBase
from ...backends.qaoa_backend import get_qaoa_backend
from ...problems import QUBO
from ...qaoa_components import (
Expand Down Expand Up @@ -171,7 +173,12 @@ def set_circuit_properties(self, **kwargs):

return None

def compile(self, problem: QUBO = None, verbose: bool = False):
def compile(
self,
problem: QUBO = None,
verbose: bool = False,
routing_function: Optional[Callable] = None,
):
"""
Initialise the trainable parameters for QAOA according to the specified
strategies and by passing the problem statement
Expand All @@ -190,7 +197,17 @@ def compile(self, problem: QUBO = None, verbose: bool = False):
verbose: bool
Set True to have a summary of QAOA to displayed after compilation
"""

# if isinstance(routing_function,Callable):
# #assert that routing_function is supported only for Standard QAOA.
# if (
# self.backend_properties.append_state is not None or\
# self.backend_properties.prepend_state is not None or\
# self.circuit_properties.mixer_hamiltonian is not 'x' or\

# )

# connect to the QPU specified
self.device.check_connection()
# we compile the method of the parent class to genereate the id and
# check the problem is a QUBO object and save it
super().compile(problem=problem)
Expand All @@ -207,7 +224,11 @@ def compile(self, problem: QUBO = None, verbose: bool = False):
)

self.qaoa_descriptor = QAOADescriptor(
self.cost_hamil, self.mixer_hamil, p=self.circuit_properties.p
self.cost_hamil,
self.mixer_hamil,
p=self.circuit_properties.p,
routing_function=routing_function,
device=self.device,
)
self.variate_params = create_qaoa_variational_params(
qaoa_descriptor=self.qaoa_descriptor,
Expand Down
6 changes: 3 additions & 3 deletions src/openqaoa-core/algorithms/workflow_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ class BackendProperties(WorkflowProperties):
The value of the CVaR parameter.
noise_model: NoiseModel
The `qiskit` noise model to be used for the shot-based simulator.
qubit_layout: Union[List[int], numpy.ndarray]
initial_qubit_mapping: Union[List[int], numpy.ndarray]
Mapping from physical to logical qubit indices, used to eventually
construct the quantum circuit. For example, for a system composed by 3 qubits
`qubit_layout=[1,3,2]`, maps `1<->0`, `3<->1`, `2<->2`, where the left hand side is the physical qubit
Expand All @@ -229,7 +229,7 @@ def __init__(
n_shots: int = 100,
cvar_alpha: float = 1,
noise_model=None,
qubit_layout: Optional[Union[List[int], np.ndarray]] = None,
initial_qubit_mapping: Optional[Union[List[int], np.ndarray]] = None,
qiskit_simulation_method: Optional[str] = None,
seed_simulator: Optional[int] = None,
active_reset: Optional[bool] = None,
Expand All @@ -243,7 +243,7 @@ def __init__(
self.append_state = append_state
self.cvar_alpha = cvar_alpha
self.noise_model = noise_model
self.qubit_layout = qubit_layout
self.initial_qubit_mapping = initial_qubit_mapping
self.seed_simulator = seed_simulator
self.qiskit_simulation_method = qiskit_simulation_method
self.active_reset = active_reset
Expand Down
Loading

0 comments on commit 7f732a6

Please sign in to comment.