Skip to content

Commit

Permalink
fixed some comments and removed circuit_routing function
Browse files Browse the repository at this point in the history
  • Loading branch information
vishal-ph committed Feb 22, 2023
1 parent e5c1463 commit 26f0c9d
Show file tree
Hide file tree
Showing 7 changed files with 540 additions and 808 deletions.
1,164 changes: 530 additions & 634 deletions examples/03_qaoa_on_qpus.ipynb

Large diffs are not rendered by default.

92 changes: 0 additions & 92 deletions src/openqaoa-core/algorithms/qaoa/qaoa_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,98 +15,6 @@
from ...optimizers.qaoa_optimizer import get_optimizer


def circuit_routing(
device: DeviceBase,
problem_to_solve: List[List[int]],
initial_mapping: Optional[List[int]] = None,
routing_algo: str = "greedy",
) -> tuple:
"""Post the HTTP request to the routing API and retrieve routed
circuit
Parameters
----------
device: DeviceBase
The device for which to route the circuit
problem_to_solve: List[List[int]]
The QUBO problem to be run on QPU
initial_mapping: Optional[List[int]]
The initial selection of qubits, if provided by the user.
Defaults to the first n-qubits on the device if `None`
routing_algo: str
The algorithm to generate an optimal SWAP network
Returns
-------
tuple[List[List],List[bool],List[int]]
The first list specifies the qubit indices for operations,
the second list differentiates between Ising and SWAP gates,
and the final list returns the final qubit mapping.
"""

device_connecivity = device.connectivity()

# params for MCTS
params = (
{
"max_depth_simulation": 10,
"max_iterations": 10,
"n_simulations": 64,
"exploration_constant": 2,
"max_tree_depth": 5,
}
if routing_algo.lower() == "mcts"
else {}
)
query = {
"algorithm": routing_algo,
"device": device_connecivity,
"problem": problem_to_solve,
"initial_mapping": initial_mapping,
"params": params,
}

# post the query via HTTP
r = requests.post(url="http://127.0.0.1:8000/qubit_routing", json=query)

results = r.json()

gate_indices_list = results["gates_list"]
initial_mapping = results["initial_mapping"]
final_mapping = results["final_mapping"]
swap_mask = results["swap_mask"]

assert len(gate_indices_list) == len(
swap_mask
), "Incorrect output from qubit routing algorithm"
assert len(final_mapping) == len(
initial_mapping
), "Incorrect number of qubits in the final qubit layout"

all_qubits = []
for pair in gate_indices_list:
all_qubits.extend(pair)
all_qubits = set(all_qubits)

#append missing qubits from the initial_mapping
initial_mapping.extend([qubit for qubit in all_qubits if qubit not in initial_mapping])
#append missing qubits from the final_mapping
final_mapping.extend([qubit for qubit in all_qubits if qubit not in final_mapping])

initial_physical_to_logical_mapping = {
qubit:idx for idx,qubit in enumerate(initial_mapping)
}

for idx,pair in enumerate(gate_indices_list):
i,j = pair
logical_i, logical_j = initial_physical_to_logical_mapping[i],initial_physical_to_logical_mapping[j]
gate_indices_list[idx] = sorted([logical_i, logical_j])

final_logical_qubit_order = [initial_physical_to_logical_mapping[q] for q in final_mapping]

return (gate_indices_list, swap_mask, initial_physical_to_logical_mapping, final_logical_qubit_order)


class QAOA(Workflow):
"""
A class implementing a QAOA workflow end to end.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,13 @@ class QAOADescriptor(AnsatzDescriptor):
mixer_block_coeffs: `List[float]`
cost_blocks: `List[RotationGateMap]`
mixer_blocks: `List[RotationGateMap]`
Properties
----------
cost_block: `List[RotationGateMap]`
mixer_block: `List[RotationGateMap]`
n_qubits: `int`
abstract_circuit: `List[RotationGateMap]`
"""
Expand Down Expand Up @@ -434,9 +436,6 @@ def route_gates_list(gates_to_route: List['GateMap'], device:'DeviceBase', routi
final_mapping
) = routing_function(device, problem_to_solve)

print("Initial mapping", initial_physical_to_logical_mapping)
print("Final mapping", final_mapping)

gates_list = [gate for gate in gates_to_route if gate.gate_label.n_qubits == 1]
swapped_history = []
for idx, pair_ij in enumerate(gate_list_indices):
Expand All @@ -462,9 +461,7 @@ def route_gates_list(gates_to_route: List['GateMap'], device:'DeviceBase', routi
raise e
ising_gate.qubit_1, ising_gate.qubit_2 = qi, qj
gates_list.append(ising_gate)

print([[gate.qubit_1] if gate.gate_label.n_qubits == 1 else [gate.qubit_1, gate.qubit_2] for gate in gates_list])


return gates_list, list(initial_physical_to_logical_mapping.keys()), final_mapping

@property
Expand Down
12 changes: 0 additions & 12 deletions src/openqaoa-core/qaoa_components/ansatz_constructor/gatemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,18 +172,6 @@ def __init__(self, qubit_1: int, qubit_2: int):
self.qubit_2 = qubit_2
self.gate_label = GateMapLabel(n_qubits=2)

# @property
# def pauli_label(self) -> List:

# return self._pauli_label

# @pauli_label.setter
# def pauli_label(self, input_label: List) -> None:

# gate_type = ['2q']
# gate_type.extend(input_label)
# self._pauli_label = gate_type

@property
def _decomposition_trivial(self) -> List[Tuple]:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,51 +51,4 @@ def repeat_gate_maps(
RotationGateMapFactory.gatemaps_layer_relabel(gatemap_list, each_layer)
)

return output_gate_list

# def remap_gate_map_labels(gatemap_list: List[RotationGateMap], input_label: List = []) -> List[RotationGateMap]:

# """
# This method reassigns the pauli_label of all the gates in the
# RotationGateMap list. The newly assigned labels help the circuit
# identify which variational angles fit into which gate.

# Parameters
# ----------
# gatemap_list: `list[RotationGateMap]`
# The list of RotationGateMap objects that needs the pauli_label attribute to be remapped.
# input_label : `list`
# Input label defining the type of gate

# Return
# ------
# `list[RotationGateMap]`
# List of RotationGateMap objects defining part of the circuit
# """

# assert type(input_label) is list, 'input_label must be of type list'

# return RotationGateMapFactory.remap_gate_map_labels(gatemap_list, input_label)

# def repeat_gate_maps_from_gate_map_list(gatemap_list: List[RotationGateMap], tag: str, n_repetitions: int) -> List[List[RotationGateMap]]:

# """
# Repeat the gates for n_repetitions layers based on the input list of RotationGateMap objects.

# Parameters
# ----------
# gatemap_list: `list[RotationGateMap]`
# The list of RotationGateMap objects that needs to be cloned.
# tag : `str`
# The tag to be used for the repeated gates
# n_repetitions: `int`
# The number of times to clone the RotationGateMap objects in the list.
# """

# output_gate_list = []

# for each_repetition in range(n_repetitions):
# output_gate_list.append(deepcopy(HamiltonianMapper.remap_gate_map_labels(
# gatemap_list, [tag.lower(), each_repetition])))

# return output_gate_list
return output_gate_list
4 changes: 0 additions & 4 deletions src/openqaoa-pyquil/backends/qaoa_pyquil_qpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,6 @@ def get_counts(self, params: QAOAVariationalBaseParams, n_shots=None) -> dict:

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

Expand Down
12 changes: 3 additions & 9 deletions src/openqaoa-qiskit/backends/qaoa_qiskit_qpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ def parametric_qaoa_circuit(self) -> QuantumCircuit:
parametric_circuit.measure(self.problem_reg,creg)
else:
for idx,qubit in enumerate(self.final_mapping[0:len(self.problem_reg)]):
cbit = creg[idx]
parametric_circuit.measure(qubit,cbit)
cbit = creg[idx]
parametric_circuit.measure(qubit,cbit)

return parametric_circuit

Expand Down Expand Up @@ -240,8 +240,7 @@ def get_counts(self, params: QAOAVariationalBaseParams, n_shots=None) -> dict:
job_state = True
no_of_api_retries += 1
time.sleep(5)
except (IBMQJobFailureError, IBMQJobInvalidStateError) as e:
raise(e)
except (IBMQJobFailureError, IBMQJobInvalidStateError):
print("There was an error with the state of the Job in IBMQ.")
no_of_job_retries += 1
break
Expand All @@ -256,11 +255,6 @@ def get_counts(self, params: QAOAVariationalBaseParams, n_shots=None) -> dict:

# Expose counts
final_counts = flip_counts(counts)
# check whether SWAP gates changed the final layout of qubits
# if self.final_mapping is not None:
# final_counts = permute_counts_dictionary(
# final_counts, self.final_mapping
# )
self.measurement_outcomes = final_counts
return final_counts

Expand Down

0 comments on commit 26f0c9d

Please sign in to comment.