Skip to content

Commit

Permalink
Add mixers to hyper_params_factory (#303)
Browse files Browse the repository at this point in the history
* add mixers to hyper_params_factory

* [pre-commit.ci] auto fixes from pre-commit.com hooks

* add missing imports

* ..math annotation

* use raw strings

* Update pyriemann_qiskit/utils/hyper_params_factory.py

Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>

* Update pyriemann_qiskit/utils/hyper_params_factory.py

Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>

* Update pyriemann_qiskit/utils/hyper_params_factory.py

Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>

* Update pyriemann_qiskit/utils/hyper_params_factory.py

Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>

---------

Co-authored-by: Gregoire Cattan <gregoire.cattan@ibm.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Quentin Barthélemy <q.barthelemy@gmail.com>
  • Loading branch information
4 people authored Sep 6, 2024
1 parent 6cd50f1 commit 6aa64d2
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 52 deletions.
3 changes: 3 additions & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ Hyper-parameters generation
gen_two_local
get_spsa
get_spsa_parameters
create_mixer_rotational_X_gates
create_mixer_rotational_XY_gates
create_mixer_rotational_XZ_gates

Preprocessing
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
57 changes: 8 additions & 49 deletions examples/toys_dataset/plot_qaoa_cv.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,60 +18,16 @@

from docplex.mp.model import Model
import matplotlib.pyplot as plt
from qiskit.circuit import QuantumCircuit, QuantumRegister
from pyriemann_qiskit.utils.hyper_params_factory import (
create_mixer_rotational_X_gates,
create_mixer_rotational_XY_gates,
create_mixer_rotational_XZ_gates,
)
from qiskit.primitives import BackendSampler
from qiskit_aer import AerSimulator
from qiskit_algorithms.optimizers import COBYLA, SPSA
from pyriemann_qiskit.utils.docplex import QAOACVOptimizer

###############################################################################
# Define mixer operators
#
# QAOA is a quantum circuit that is a repetition of
# a cost an mixer operator.
#
# We will use some of the mixer operators defined in [1]_.
#


def create_mixer_rotational_X_gates(angle):
def mixer_X(n_qubits):
qr = QuantumRegister(n_qubits)
mixer = QuantumCircuit(qr)

for i in range(n_qubits):
mixer.rx(angle, qr[i])

return mixer_X


def create_mixer_rotational_XY_gates(angle):
def mixer_XY(n_qubits):
qr = QuantumRegister(n_qubits)
mixer = QuantumCircuit(qr)

for i in range(n_qubits - 1):
mixer.rx(angle, qr[i])
mixer.rx(angle, qr[i + 1])
mixer.ry(angle, qr[i])
mixer.ry(angle, qr[i + 1])

return mixer_XY


def create_mixer_rotational_XZ_gates(angle):
def mixer_XZ(n_qubits):
qr = QuantumRegister(n_qubits)
mixer = QuantumCircuit(qr)

for i in range(1, n_qubits - 1):
mixer.rz(angle, qr[i - 1])
mixer.rx(angle, qr[i])
mixer.rx(angle + math.pi / 2, qr[i])
mixer.rz(angle, qr[i + 1])

return mixer_XZ


###############################################################################
# Run QAOA-CV
Expand Down Expand Up @@ -121,6 +77,9 @@ def run_qaoa_cv(n_reps, optimizer, create_mixer):
###############################################################################
# Hyper-parameters
#
# QAOA is a quantum circuit that is a repetition of
# a cost an mixer operator.
#
# We will now try different combination of optimizer and mixers.
#

Expand Down
2 changes: 2 additions & 0 deletions pyriemann_qiskit/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
square_bin_mat_var,
ClassicalOptimizer,
NaiveQAOAOptimizer,
QAOACVOptimizer,
set_global_optimizer,
get_global_optimizer,
)
Expand Down Expand Up @@ -36,6 +37,7 @@
"square_bin_mat_var",
"ClassicalOptimizer",
"NaiveQAOAOptimizer",
"QAOACVOptimizer",
"set_global_optimizer",
"get_global_optimizer",
"distance",
Expand Down
133 changes: 130 additions & 3 deletions pyriemann_qiskit/utils/hyper_params_factory.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from qiskit.circuit.library import ZZFeatureMap, ZFeatureMap, PauliFeatureMap
from qiskit_algorithms.optimizers import SPSA
from qiskit.circuit.library import TwoLocal
import inspect
import math

from qiskit.circuit import QuantumCircuit, QuantumRegister
from qiskit.circuit.library import (
ZZFeatureMap,
ZFeatureMap,
PauliFeatureMap,
TwoLocal,
)
from qiskit_algorithms.optimizers import SPSA


def gen_x_feature_map(reps=2):
Expand Down Expand Up @@ -335,3 +342,123 @@ def get_spsa_parameters(spsa):
signature.parameters["gamma"].default,
signature.parameters["stability_constant"].default,
)


def create_mixer_rotational_X_gates(angle):
r"""Return the default mixing operator with QAOA.
.. math::
H_X = \sum_{i}^{N} X_i
See [1]_ for details.
Parameters
----------
angle : float
The angle of the gates' rotation.
Returns
-------
mixer : QuantumCircuit
The mixer.
Notes
-----
.. versionadded:: 0.4.0
References
----------
.. [1] \
https://dice.cyfronet.pl/papers/JPlewa_JSienko_msc_v2.pdf
"""

def mixer_X(n_qubits):
qr = QuantumRegister(n_qubits)
mixer = QuantumCircuit(qr)

for qr_ in qr:
mixer.rx(angle, qr_)

return mixer_X


def create_mixer_rotational_XY_gates(angle):
r"""Return the XY mixer.
.. math::
H_{XY} = \sum_{i}^{N-1} \left( X_i X_{i+1} + Y_i Y_{i+1} \right),
See [1]_ for details.
Parameters
----------
angle : float
The angle of the gates' rotation.
Returns
-------
mixer : QuantumCircuit
The mixer.
Notes
-----
.. versionadded:: 0.4.0
References
----------
.. [1] \
https://dice.cyfronet.pl/papers/JPlewa_JSienko_msc_v2.pdf
"""

def mixer_XY(n_qubits):
qr = QuantumRegister(n_qubits)
mixer = QuantumCircuit(qr)

for i in range(n_qubits - 1):
mixer.rx(angle, qr[i])
mixer.rx(angle, qr[i + 1])
mixer.ry(angle, qr[i])
mixer.ry(angle, qr[i + 1])

return mixer_XY


def create_mixer_rotational_XZ_gates(angle):
r"""Return a mixing operator with XZ gates.
.. math::
H_{\text{mix}} = \sum_{i}^{N-1} \left( Z_{i-1} X_i - X_i Z_{i+1} \right).
See [1]_ for details.
Parameters
----------
angle : float
The angle of the gates' rotation.
Returns
-------
mixer : QuantumCircuit
The mixer.
Notes
-----
.. versionadded:: 0.4.0
References
----------
.. [1] \
https://dice.cyfronet.pl/papers/JPlewa_JSienko_msc_v2.pdf
"""

def mixer_XZ(n_qubits):
qr = QuantumRegister(n_qubits)
mixer = QuantumCircuit(qr)

for i in range(1, n_qubits - 1):
mixer.rz(angle, qr[i - 1])
mixer.rx(angle, qr[i])
mixer.rx(angle + math.pi / 2, qr[i])
mixer.rz(angle, qr[i + 1])

return mixer_XZ

0 comments on commit 6aa64d2

Please sign in to comment.