<jupyter_start><jupyter_text>古典最適化アルゴリズムを設定する ここでは、量子古典ハイブリッド計算であるVQEやQAOAで利用される古典最適化アルゴリズムについて、古典最適化アルゴリズムの設定方法をみてみたいと思います。  Blueqatのインストールインストールはpipから簡単に行うことができます。<jupyter_code>!pip install blueqat<jupyter_output>Requirement already satisfied: blueqat in /home/ec2-user/anaconda3/envs/python3/lib/python3.6/site-packages (0.3.13)
Requirement already satisfied: numpy~=1.12 in /home/ec2-user/anaconda3/envs/python3/lib/python3.6/site-packages (from blueqat) (1.18.3)
Requirement already satisfied: scipy>=1.1.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.6/site-packages (from blueqat) (1.1.0)
[33mWARNING: You are using pip version 20.0.2; however, version 20.1 is available.
You should consider upgrading via the '/home/ec2-user/anaconda3/envs/python3/bin/python -m pip install --upgrade pip' command.[0m<jupyter_text>設定方法古典最適化アルゴリズムは、ansatzの実行の際にminimizerを指定するだけで変更できます。古典最適化のアルゴリズムは指定できます。主にscipyから選べます。https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html'Nelder-Mead', 'Powell', 'CG', 'BFGS', 'L-BFGS-B', 'TNC', 'COBYLA', 'SLSQP'<jupyter_code>import numpy as np
from blueqat import Circuit
from blueqat.pauli import X, Y, Z, I
from blueqat.pauli import qubo_bit as q
from blueqat.vqe import AnsatzBase, Vqe, get_scipy_minimizer

class QubitAnsatz(AnsatzBase):
    def __init__(self, hamiltonian):
        super().__init__(hamiltonian, 4)
        self.step = 1

    def get_circuit(self, params):
        a, b, c, d = params
        return Circuit().ry(a)[0].rz(b)[0].ry(c)[1].rz(d)[1]

h = -3*q(0)-3*q(1)-2*q(0)*q(1)
h = h.to_expr().simplify()
minimizer = get_scipy_minimizer(method="COBYLA",options={"tol":5.0e-4})
runner = Vqe(QubitAnsatz(h),minimizer=minimizer)
result = runner.run()

print('Result by VQE')
print(runner.ansatz.get_energy_sparse(result.circuit))

# Hamiltonian to matrix
mat = h.to_matrix()

# Calculate by numpy
print('Result by numpy')
print(np.linalg.eigh(mat)[0][0])<jupyter_output>Result by VQE
-7.999999839073751
Result by numpy
-8.0<jupyter_text>その他の最適化ソルバーの実装その他、自分で使いたい最適化ソルバーを実装するには下記のように関数を用意して、minimizerとして利用します。今回はベイズ最適化を対象として、hyperoptを導入してみます。<jupyter_code>!pip install hyperopt
def hyperopt_minimizer(objective, n_params):
    from hyperopt import fmin, Trials, tpe, hp
    trials = Trials()
    best = fmin(objective, [hp.uniform(f'p{i}', 0., 2 * np.pi) for i in range(n_params)],
            algo=tpe.suggest, max_evals=100, trials=trials, verbose=1)
    return list(best.values())
runner = Vqe(QubitAnsatz(h),minimizer=hyperopt_minimizer)
result = runner.run()

print('Result by VQE')
print(runner.ansatz.get_energy_sparse(result.circuit))

# Hamiltonian to matrix
mat = h.to_matrix()

# Calculate by numpy
print('Result by numpy')
print(np.linalg.eigh(mat)[0][0])<jupyter_output>100%|██████████| 100/100 [00:00<00:00, 206.96trial/s, best loss: -7.990075559549002]
Result by VQE
-7.990075559549002
Result by numpy
-8.0
{'fraction_non_alphanumeric': 0.1416264645072364, 'fraction_numerical': 0.04996554100620262, 'mean_word_length': 5.911904761904762, 'pattern_counts': {'":': 1, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '145043', 'license': 'Apache-2.0', 'n_tokens_mistral': 1452, 'n_tokens_neox': 1339, 'n_words': 203, 'path': 'tutorial-ja/701_optimizer_ja.ipynb', 'repo_name': 'tmdoi/Blueqat-tutorials', 'size': '2902'}