-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update plot to seaborn, add QAOA example
- Loading branch information
Showing
13 changed files
with
121 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
"""Maxcut example.""" | ||
|
||
import numpy as np | ||
from qiskit.algorithms.minimum_eigen_solvers.qaoa.var_form import QAOAVarForm | ||
from qiskit.circuit import QuantumCircuit, ParameterVector | ||
from qiskit.circuit.parametertable import ParameterTable | ||
from qiskit.opflow import Z, I, H | ||
|
||
from .benchmark import Benchmark | ||
|
||
class QAOAAnsatz(QuantumCircuit): | ||
"""QAOA ansatz as a quantum circuit.""" | ||
|
||
def __init__(self, operator, reps=1): | ||
self._reps = reps | ||
self._operator = operator | ||
super().__init__(operator.num_qubits) | ||
self._build() | ||
|
||
@property | ||
def reps(self): | ||
"""Get the number of repetitions of the circuit.""" | ||
return self._reps | ||
|
||
@reps.setter | ||
def reps(self, value): | ||
"""Set the number of repetitions. Rebuilds the circuit.""" | ||
self._reps = value | ||
self._build() # rebuild | ||
|
||
def assign_parameters(self, params, inplace=False): | ||
"""Assign parameters.""" | ||
if isinstance(params, (list, np.ndarray)): | ||
params = dict(zip(self._params[:], params)) | ||
|
||
return super().assign_parameters(params, inplace=inplace) | ||
|
||
@property | ||
def ordered_parameters(self): | ||
return self._params[:] | ||
|
||
def _build(self): | ||
# wipe current state | ||
self._data = [] | ||
self._parameter_table = ParameterTable() | ||
|
||
# get QAOA circuit | ||
qaoa = QAOAVarForm(self._operator, self._reps) | ||
params = ParameterVector('th', qaoa.num_parameters) | ||
circuit = qaoa.construct_circuit(params) | ||
|
||
# store the parameters in a list for assigning them | ||
self._params = params | ||
|
||
|
||
# combine the circuit | ||
self.compose(circuit, inplace=True) | ||
|
||
|
||
def run_maxcut(): | ||
operator = (I ^ I ^ Z ^ Z) + (I ^ Z ^ I ^ Z) + (Z ^ I ^ I ^ Z) + (I ^ Z ^ Z ^ I) | ||
circuit = QAOAAnsatz(operator) | ||
|
||
benchmark = Benchmark(2 ** np.arange(2, 8), H, 24) | ||
benchmark.run_benchmark(circuit, 'free') | ||
benchmark.plot(show=True) |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,38 @@ | ||
"""Script to run the benchmarks.""" | ||
|
||
# pylint: disable=invalid-name | ||
|
||
import sys | ||
from benchmarks.benchmark import Benchmark | ||
from benchmarks.efficient_su2 import run_efficientsu2 | ||
from benchmarks.featuremap import run_featuremap | ||
from benchmarks.maxcut import run_maxcut | ||
|
||
if len(sys.argv) < 2: | ||
print('Please specify a benchmark to run. Available: efficientsu2 featuremap maxcut') | ||
sys.exit() | ||
|
||
task = sys.argv[1] | ||
|
||
if task == 'plot': | ||
if len(sys.argv) < 3: | ||
print('To plot please specify the filename as second argument.') | ||
sys.exit() | ||
fname = sys.argv[2] | ||
|
||
if len(sys.argv) != 2: | ||
print('Please specify a benchmark to run. Available: efficientsu2 uccsd') | ||
if len(sys.argv) > 3: | ||
saveas = sys.argv[3] | ||
else: | ||
saveas = None | ||
|
||
if sys.argv[1] == 'efficientsu2': | ||
if task == 'efficientsu2': | ||
run_efficientsu2() | ||
elif sys.argv[1] == 'featuremap': | ||
elif task == 'featuremap': | ||
run_featuremap() | ||
elif task == 'maxcut': | ||
run_maxcut() | ||
elif task == 'plot': | ||
benchmark = Benchmark([0], None, None) | ||
benchmark.plot(fname, saveas=saveas, show=True) | ||
else: | ||
raise ValueError(f'Invalid benchmark name: {sys.argv[1]}') | ||
raise ValueError(f'Invalid argument: {task}') |