Exemple #1
0
    # Add one entanglement not to have two adjacent input
    bdr.cz(0, 1)

    # The block repeats with the other parameters
    block(bdr, x, params[4:])

    return bdr


##############################
# We instantiate and train the classifier

nbqbits = 2
nbparams = 8

qc = mqCircuitML(make_circuit=irisCircuit, nbqbits=nbqbits, nbparams=nbparams)

bitstr = ['00', '01', '10']

model = Classifier(qc, bitstr).fit(input_train, target_train, method="BFGS")

##############################
# We test it using qiskit

backend = Backends("ibmq_ourense", hub="ibm-q")

qc = qkCircuitML(
    make_circuit=irisCircuit,
    nbqbits=nbqbits,
    nbparams=nbparams,
    backend=backend,
Exemple #2
0
        
        bdr.cz(0,1).allin(x[[4,5]])
        bdr.cz(0,1).allin(p[[4,5]])
        
        bdr.cz(0,1).allin(x[[6,7]])
        bdr.cz(0,1).allin(p[[6,7]])
        
        bdr.cz(0,1).allin(x[[8,9]])
        bdr.cz(0,1).allin(p[[8,9]])
        
        bdr.cz(0,1).input(0, x[10])
        bdr.cz(0,1).input(0, p[10])
        
    def wineCircuit(bdr, x, params):

        block(bdr, x, params[:11])
        bdr.cz(0,1)
        block(bdr, x, params[11:])
        
        return bdr

    input_train = np.random.rand(2940,11)
    target_train = (3*np.random.rand((2940))).astype(int)

    qc = mqCircuitML(make_circuit=wineCircuit,
                    nbqbits=nbqbits, nbparams=nbparams)

    model = Classifier(qc, bitstr)

    model.fit(input_train, target_train)

def simple_circuit(bdr, x, params):
    bdr.allin(x).cz(0, 1).allin(params[:2])
    bdr.cz(0, 1).allin(params[2:4])
    return bdr


##############################
# Now we instantiate the circuit

nbqbits = 2
nbparams = 4

qc = mqCircuitML(make_circuit=simple_circuit,
                 nbqbits=nbqbits,
                 nbparams=nbparams)

bitstr = ['00', '11']

# We can use exact probabilities
model = Classifier(qc, bitstr)
model.fit(X, y, save_loss_progress=True)

# Or pass though shots-based estimates
model2 = Classifier(qc, bitstr, nbshots=300)
model2.fit(X, y, method='COBYLA', save_loss_progress=True)

if FIGURES:
    fig, ax = plt.subplots(figsize=(5, 3))
    fig.set_tight_layout(True)
if not isdir("figures-ex"):
    from os import mkdir
    mkdir("figures-ex")
#############################################

from polyadicqml.manyq import mqCircuitML
import numpy as np

# `make_circuit` can have any name
def simple_circuit(bdr, x, params):
    return bdr.allin(x).cz(0,1).allin(params)

# We instantiate the circuitML
from polyadicqml.manyq import mqCircuitML

circuit = mqCircuitML(simple_circuit, nbqbits=2, nbparams=2)

# We create an input matrix and a param vector
import numpy as np

X = np.array([[-np.pi/4, np.pi/4],
            [np.pi/4, -np.pi/4]])
params = np.array([np.pi/2, -np.pi/2])

probs = circuit.run(X, params)
counts = circuit.run(X, params, nbshots=100)

import matplotlib.pyplot as plt
import seaborn as sns
sns.set()