Пример #1
0
def main_tfq():
    """
    DQAS with tensorflow quantum engine
    unitary learning example

    :return:
    """
    p = 4
    cset = [
        cirq.H(cirq.GridQubit(0, 0)),
        cirq.H(cirq.GridQubit(1, 0)),
        cirq.CNOT(cirq.GridQubit(0, 0), cirq.GridQubit(1, 0)),
        cirq.CNOT(cirq.GridQubit(1, 0), cirq.GridQubit(0, 0)),
        cirq.X(cirq.GridQubit(0, 0)),
        cirq.X(cirq.GridQubit(1, 0)),
    ]
    set_op_pool(cset)
    c = len(cset)
    stp, nnp, _ = DQAS_search(
        GHZ_vag_tfq,
        g=double_qubits_initial(),
        p=p,
        batch=16,
        epochs=5,
        verbose=False,
        parallel_num=0,
        nnp_initial_value=np.zeros([p, c]),
        structure_opt=tf.keras.optimizers.Adam(learning_rate=0.15),
    )
    preset = get_preset(stp).numpy()
    GHZ_vag_tfq(double_qubits_initial().send(None), nnp, preset, verbose=True)
Пример #2
0
def main_tn():
    """
    DQAS with tensorcircuit engine backend by TensorNetwork
    state preparation example

    :return:
    """
    # multi start may be necessary
    ghz_pool = [
        ("ry", 0),
        ("ry", 1),
        ("ry", 2),
        ("CNOT", 0, 1),
        ("CNOT", 1, 0),
        ("CNOT", 0, 2),
        ("CNOT", 2, 0),
        ("H", 0),
        ("H", 1),
        ("H", 2),
    ]
    set_op_pool(ghz_pool)
    c = len(ghz_pool)
    p = 4
    stp, nnp, _ = DQAS_search(
        GHZ_vag,
        p=p,
        batch=128,
        epochs=200,
        verbose=True,
        parallel_num=0,
        nnp_initial_value=np.zeros([p, c]),
        structure_opt=tf.keras.optimizers.Adam(learning_rate=0.15),
    )
    preset = get_preset(stp).numpy()
    GHZ_vag(None, nnp, preset, verbose=True)
Пример #3
0
def main_4():
    qft_4 = partial(qft_qem_vag, n=4)

    set_op_pool(
        [
            cirq.I,
            cirq.X,
            cirq.Y,
            cirq.Z,
            cirq.H,
            cirq.rx(np.pi / 3),
            cirq.rx(np.pi * 2.0 / 3),
            cirq.rz(np.pi / 3),
            cirq.rz(np.pi * 2.0 / 3),
            cirq.S,
            cirq.T,
        ]
    )
    DQAS_search(
        qft_4,
        nnp_initial_value=tf.zeros([12, 11]),
        p=12,
        prethermal=0,
        batch=32,
        verbose=False,
        verbose_func=verbose_output,
        epochs=6,
    )
Пример #4
0
def main_3():
    qft_3 = partial(qft_qem_vag, n=3)

    set_op_pool([cirq.X, cirq.Y, cirq.Z, cirq.I, cirq.T, cirq.S])
    DQAS_search(
        qft_3,
        nnp_initial_value=tf.zeros([6, 6]),
        p=6,
        prethermal=0,
        batch=32,
        verbose=False,
        epochs=30,
    )
Пример #5
0
        structure_opt=tf.keras.optimizers.SGD(learning_rate=learning_rate_sch),
    )

    with open("qaoa_block.result", "wb") as f:
        dump([stp.numpy(), nnp.numpy(), h], f)

    epochs = np.arange(len(h))
    data = np.array([r.loss for r in h])
    plt.plot(epochs, data)
    plt.xlabel("epoch")
    plt.ylabel("objective")
    plt.savefig("qaoa_block.pdf")


layer_pool = [Hlayer, rxlayer, rylayer, rzlayer, xxlayer, yylayer, zzlayer]
block_pool = [
    Hlayer,
    rx_zz_block,
    zz_ry_block,
    zz_rx_block,
    zz_rz_block,
    xx_rz_block,
    yy_rx_block,
    rx_rz_block,
]
set_op_pool(block_pool)

if __name__ == "__main__":
    # main_layerwise_encoding()
    main_block_encoding()
Пример #6
0
import sys

sys.path.insert(0, "../")

import numpy as np
import tensorflow as tf
import tensorcircuit as tc
from tensorcircuit.applications.dqas import (
    parallel_qaoa_train,
    single_generator,
    set_op_pool,
)
from tensorcircuit.applications.layers import *
from tensorcircuit.applications.graphdata import get_graph

tc.set_backend("tensorflow")

set_op_pool([Hlayer, rxlayer, rylayer, rzlayer, xxlayer, yylayer, zzlayer])
if __name__ == "__main__":
    parallel_qaoa_train(
        [0, 6, 1, 6, 1],
        single_generator(get_graph("8B")),
        tries=4,
        cores=2,
        batch=1,
        epochs=5,
        scale=0.8,
    )