Esempio n. 1
0
def decrementExperiment():
    """ 
    This is the main method for the decrement problem 
    This problem is phrased as follows:
    given a set of registers, we will create a prepare statement corresponding to a value in superposition, such as 
    0x4, the decrement op should output 50% for 0 and 5 correspondingly
    
    We will implement the experiment for 5 qubits (+1 qubit as scratch): 
    the first qubit is the min value within the circuit initially: x a[0] 
    (as in the prepareStatement)
    So the values in the lookup table are : 
    h a[1] (2^1) - {'00000': 0.5, '00010': 0.5}
    h a[2] (2^2) - {'00000': 0.5, '00100': 0.5}
    h a[3] (2^3) - {'00000': 0.5, '01000': 0.5}
    h a[4] (2^4) - {'00000': 0.5, '10000': 0.5}

    """
    #sets up entries in the lookup table
    lookupTable = [
        ('00000', '00010', "x q[0];\nh q[1];\nrz(0.785398163397448) q[1];\n"),
        ('00000', '00100', "x q[0];\nh q[2];\nrz(0.785398163397448) q[2];\n"),
        ('00000', '01000', "x q[0];\nh q[3];\nrz(0.785398163397448) q[3];\n"),
        ('00000', '10000', "x q[0];\nh q[4];\nrz(0.785398163397448) q[4];\n"),
    ]

    #this array makes it easy to control which experiment is to run by the sim
    enabledExperiments = [False, False, True, False]
    #default params
    params = GAParameters(MProb, CProb, totalEpochs)
    incDecExp = IncDecExperiment(registers, lookupTable, r, popSize)

    if (enabledExperiments[0]):
        # the default roulette wheel selection with a constant CProb, MProb, 80% retention rate
        try:
            ga = Ga(r,
                    popSize,
                    registers,
                    generationFunction=QASMPrimitiveInstruction,
                    minSize=minSize,
                    maxSize=maxSize)
            stats = ga.evolve(totalEpochs, probCrossover = CProb, probMutation= MProb, \
                        evaluationFunc = incDecExp.evaluationFunction, selectionFunc= incDecExp.rouletteWheelSelection, \
                        params = params)
        except Exception:
            traceback.print_exc()
        generateSummaries(stats=stats, experimentName="dec-none")

    if (enabledExperiments[1]):
        # decreasing CProb, MProb with default roulette wheel selection
        try:
            params = GAParameters(MProb, CProb, totalEpochs, decreaseM = True, decreaseC = True, \
                        MStartEpoch = mStartEpoch, MEndEpoch = mEndEpoch, CStartEpoch = cStartEpoch, CEndEpoch = cEndEpoch)
            ga = Ga(r,
                    popSize,
                    registers,
                    generationFunction=QASMPrimitiveInstruction,
                    minSize=minSize,
                    maxSize=maxSize)
            stats = ga.evolve(totalEpochs, probCrossover = CProb, probMutation= MProb, \
                        evaluationFunc = incDecExp.evaluationFunction, selectionFunc= incDecExp.rouletteWheelSelection, \
                        params = params)
        except Exception:
            traceback.print_exc()
        generateSummaries(stats=stats, experimentName="dec-MC")

    if (enabledExperiments[2]):
        try:
            # decreasing CProb, MProb with default roulette wheel selection but delta mutation func
            #params = GAParameters(MProb, CProb, totalEpochs, decreaseM = True, decreaseC = True, \
            #           MStartEpoch = mStartEpoch, MEndEpoch = mEndEpoch, CStartEpoch = cStartEpoch, CEndEpoch = cEndEpoch)

            #constant CProb, MProb with default roulette wheel selection but delta mutation func
            params = GAParameters(MProb, CProb, totalEpochs, decreaseM = False, decreaseC = False, \
                MStartEpoch = mStartEpoch, MEndEpoch = mEndEpoch, CStartEpoch = cStartEpoch, CEndEpoch = cEndEpoch)
            ga = Ga(r,
                    popSize,
                    registers,
                    generationFunction=QASMPrimitiveInstruction,
                    minSize=minSize,
                    maxSize=maxSize)
            stats = ga.evolve(totalEpochs, probCrossover = CProb, probMutation= MProb, \
                        evaluationFunc = incDecExp.evaluationFunction, selectionFunc= incDecExp.rouletteWheelSelection, \
                        mutationArgFunc = deltaMutationFunc, params = params)
        except Exception:
            traceback.print_exc()
        generateSummaries(stats=stats, experimentName="dec-rw")

    if (enabledExperiments[3]):
        try:
            # decreasing CProb, MProb with default roulette wheel selection but detal mutation func
            #params = GAParameters(MProb, CProb, totalEpochs, decreaseM = True, decreaseC = True, \
            # MStartEpoch = mStartEpoch, MEndEpoch = mEndEpoch, CStartEpoch = cStartEpoch, CEndEpoch = cEndEpoch, \
            # eliteRatio=eliteRatio)
            params = GAParameters(MProb, CProb, totalEpochs, decreaseM = False, decreaseC = False, \
                MStartEpoch = mStartEpoch, MEndEpoch = mEndEpoch, CStartEpoch = cStartEpoch, CEndEpoch = cEndEpoch, \
                eliteRatio=eliteRatio)
            ga = Ga(r,
                    popSize,
                    registers,
                    generationFunction=QASMPrimitiveInstruction,
                    minSize=minSize,
                    maxSize=maxSize)
            stats = ga.evolve(totalEpochs, probCrossover = CProb, probMutation= MProb, \
                        evaluationFunc = incDecExp.evaluationFunction, selectionFunc= incDecExp.elitismSelection, \
                        mutationArgFunc = deltaMutationFunc, params = params)
        except Exception:
            traceback.print_exc()
        generateSummaries(stats=stats, experimentName="dec-e")
    input("Press [enter] to continue.")
Esempio n. 2
0
def main():
    # this is the "prepare" statements used by the maxone problem experiments
    hadamardPattern = ""
    # places all gates in hadamard
    for i in range(0, registers):
        hadamardPattern = hadamardPattern + "h q[" + str(i) + "];\n"

    #this is the first experiment:
    #Roulette wheel selection,
    #default multipoint crossover, mutation, and selection function is based on
    #the binary string problem and how close the value is to the intended 1111 string value.
    #1/(max value - prob of the largest binary string in the quantum experiment) where max value is 2^ num of register - 1 (the max value
    #for a binary string represented by the num of registers).
    params = GAParameters(MProb,
                          CProb,
                          totalEpochs,
                          openQASMPrepareStatement=hadamardPattern)
    try:
        ga = Ga(r,
                popSize,
                registers,
                generationFunction=QASMPrimitiveInstruction,
                minSize=minSize,
                maxSize=maxSize)
        stats = ga.evolve(totalEpochs,
                          probCrossover=CProb,
                          probMutation=MProb,
                          params=params)
    except Exception:
        traceback.print_exc()
    generateSummaries(stats=stats, experimentName="none")

    #this is the 2nd experiment:
    #Roulette wheel selection,
    #default multipoint crossover, mutation, selection function is, again, based on the binary string
    #problem, however, the fitness value is weighted based on the following formulation:
    #prob1 * value of the first binary string + prob2 * value of the 2nd binary string + ...
    params = GAParameters(MProb,
                          CProb,
                          totalEpochs,
                          openQASMPrepareStatement=hadamardPattern)

    try:
        ga = Ga(r,
                popSize,
                registers,
                generationFunction=QASMPrimitiveInstruction,
                minSize=minSize,
                maxSize=maxSize)
        stats = ga.evolve(totalEpochs,
                          selectionFunc=weightedSelectionFunc,
                          probCrossover=CProb,
                          probMutation=MProb,
                          params=params)
    except Exception:
        traceback.print_exc()
    generateSummaries(stats=stats, experimentName="W")

    #This is the start of the 3rd experiment with decreasing M and C probabilities
    #Default roulette selection method
    params = GAParameters(MProb, CProb, totalEpochs, decreaseM = True, decreaseC = True, \
                MStartEpoch = mStartEpoch, MEndEpoch = mEndEpoch, CStartEpoch = cStartEpoch, CEndEpoch = cEndEpoch, \
                openQASMPrepareStatement = hadamardPattern)
    try:
        ga = Ga(r,
                popSize,
                registers,
                generationFunction=QASMPrimitiveInstruction,
                minSize=minSize,
                maxSize=maxSize)
        stats = ga.evolve(totalEpochs,
                          probCrossover=CProb,
                          probMutation=MProb,
                          params=params)
    except Exception:
        traceback.print_exc()
    generateSummaries(stats=stats, experimentName="dMC")

    #This is the start of the 4th experiment with decreasing M and C probabilities
    #weight average selection method
    params = GAParameters(MProb, CProb, totalEpochs, decreaseM = True, decreaseC = True, \
                MStartEpoch = mStartEpoch, MEndEpoch = mEndEpoch, CStartEpoch = cStartEpoch, CEndEpoch = cEndEpoch, \
                openQASMPrepareStatement = hadamardPattern)
    try:
        ga = Ga(r,
                popSize,
                registers,
                generationFunction=QASMPrimitiveInstruction,
                minSize=minSize,
                maxSize=maxSize)
        stats = ga.evolve(totalEpochs,
                          selectionFunc=weightedSelectionFunc,
                          probCrossover=CProb,
                          probMutation=MProb,
                          params=params)
    except Exception:
        traceback.print_exc()
    generateSummaries(stats=stats, experimentName="dMCW")

    #This is the start of the 5th experiment with decreasing M and C probabilities
    #weight average selection method and delta mutation func

    params = GAParameters(MProb, CProb, totalEpochs, decreaseM = True, decreaseC = True, \
                MStartEpoch = mStartEpoch, MEndEpoch = mEndEpoch, CStartEpoch = cStartEpoch, CEndEpoch = cEndEpoch, \
                openQASMPrepareStatement = hadamardPattern)
    try:
        ga = Ga(r,
                popSize,
                registers,
                generationFunction=QASMPrimitiveInstruction,
                minSize=minSize,
                maxSize=maxSize)
        stats = ga.evolve(totalEpochs, selectionFunc = weightedSelectionFunc, probCrossover = CProb, \
            probMutation= MProb, mutationArgFunc = deltaMutationFunc, params = params)
    except Exception:
        traceback.print_exc()
    generateSummaries(stats=stats, experimentName="dMCW")

    #test code for elitism
    params = GAParameters(MProb, CProb, totalEpochs, decreaseM = True, decreaseC = True, \
                MStartEpoch = mStartEpoch, MEndEpoch = mEndEpoch, CStartEpoch = cStartEpoch, CEndEpoch = cEndEpoch, \
                eliteRatio=eliteRatio , openQASMPrepareStatement = hadamardPattern)
    try:
        ga = Ga(r,
                popSize,
                registers,
                generationFunction=QASMPrimitiveInstruction,
                minSize=minSize,
                maxSize=maxSize)
        stats = ga.evolve(totalEpochs, selectionFunc = elitismDefaultSelectionFunc, probCrossover = CProb, \
            probMutation= MProb, mutationArgFunc = deltaMutationFunc, params = params)
    except Exception:
        traceback.print_exc()
    generateSummaries(stats=stats, experimentName="edMCW")
    input("Press [enter] to continue.")
Esempio n. 3
0
def incrementExperiment():
    """ 
    This is the main method for the increment problem 
    This problem is phrased as follows:
    given a set of registers, we will create a prepare statement corresponding to a value in superposition, such as 
    4, the increment op should output 50% for 1 and 5 correspondingly
    
    The setup is the same as what's depicted in : https://oreilly-qc.github.io/# (increment & decrement)
    but we will use a[5] instead of a[4] and scratch[1] in the preparestatement. Thus, wherever there's a scratch[0], 
    we will have a[4] instead. If you look at the test code in OpenQASMTest.py, the output should be the same.

    We will implement the experiment for 5 qubits (+1 qubit as scratch): 
    the first qubit is the min value within the circuit: x a[0] 
    (as in the prepareStatement)
    So the values in the lookup table are : 
    h a[1] (2^1) - {'00010': 0.5, '00100': 0.5}
    h a[2] (2^2) - {'00010': 0.5, '00110': 0.5}
    h a[3] (2^3) - {'00010': 0.5, '01010': 0.5}
    h a[4] (2^4) - {'00010': 0.5, '10010': 0.5}
    Note that the lowest value is 00010 = 2 because we wrote a value of 1 in x q[0].

    """
    #sets up entries in the lookup table
    lookupTable = [
        ('00010', '00100', "x q[0];\nh q[1];\nrz(0.785398163397448) q[1];\n"),
        ('00010', '00110', "x q[0];\nh q[2];\nrz(0.785398163397448) q[2];\n"),
        ('00010', '01010', "x q[0];\nh q[3];\nrz(0.785398163397448) q[3];\n"),
        ('00010', '10010', "x q[0];\nh q[4];\nrz(0.785398163397448) q[4];\n"),
    ]

    #this array makes it easy to control which experiment is to run by the sim
    enabledExperiments = [False, False, False, True]
    #default params
    params = GAParameters(MProb, CProb, totalEpochs)

    incDecExp = IncDecExperiment(registers, lookupTable, r, popSize)

    if (enabledExperiments[0]):
        # the default roulette wheel selection with a constant CProb, MProb, 80% retention rate
        try:
            ga = Ga(r,
                    popSize,
                    registers,
                    generationFunction=QASMPrimitiveInstruction,
                    minSize=minSize,
                    maxSize=maxSize)
            stats = ga.evolve(totalEpochs, probCrossover = CProb, probMutation= MProb, \
                        evaluationFunc = incDecExp.evaluationFunction, selectionFunc= incDecExp.rouletteWheelSelection, \
                        params = params)
        except Exception:
            traceback.print_exc()
        generateSummaries(stats=stats, experimentName="inc-none")

    if (enabledExperiments[1]):

        try:
            # decreasing CProb, MProb with default roulette wheel selection

            # params = GAParameters(MProb, CProb, totalEpochs, decreaseM = True, decreaseC = True, \
            #            MStartEpoch = mStartEpoch, MEndEpoch = mEndEpoch, CStartEpoch = cStartEpoch, CEndEpoch = cEndEpoch)

            #constant CProb, MProb with default roulette wheel selection
            params = GAParameters(MProb, CProb, totalEpochs, decreaseM = False, decreaseC = False, \
                        MStartEpoch = mStartEpoch, MEndEpoch = mEndEpoch, CStartEpoch = cStartEpoch, CEndEpoch = cEndEpoch)
            ga = Ga(r,
                    popSize,
                    registers,
                    generationFunction=QASMPrimitiveInstruction,
                    minSize=minSize,
                    maxSize=maxSize)
            stats = ga.evolve(totalEpochs, probCrossover = CProb, probMutation= MProb, \
                        evaluationFunc = incDecExp.evaluationFunction, selectionFunc= incDecExp.rouletteWheelSelection, \
                        params = params)
        except Exception:
            traceback.print_exc()
        generateSummaries(stats=stats, experimentName="inc-MC")

    if (enabledExperiments[2]):

        try:
            # decreasing CProb, MProb with default roulette wheel selection but delta mutation func
            #params = GAParameters(MProb, CProb, totalEpochs, decreaseM = True, decreaseC = True, \
            #            MStartEpoch = mStartEpoch, MEndEpoch = mEndEpoch, CStartEpoch = cStartEpoch, CEndEpoch = cEndEpoch)

            #constant cprob, mprob with default roulette wheel but delta mutation func
            params = GAParameters(MProb, CProb, totalEpochs, decreaseM = False, decreaseC = False, \
                        MStartEpoch = mStartEpoch, MEndEpoch = mEndEpoch, CStartEpoch = cStartEpoch, CEndEpoch = cEndEpoch)
            ga = Ga(r,
                    popSize,
                    registers,
                    generationFunction=QASMPrimitiveInstruction,
                    minSize=minSize,
                    maxSize=maxSize)
            stats = ga.evolve(totalEpochs, probCrossover = CProb, probMutation= MProb, \
                        evaluationFunc = incDecExp.evaluationFunction, selectionFunc= incDecExp.rouletteWheelSelection, \
                        mutationArgFunc = deltaMutationFunc, params = params)
        except Exception:
            traceback.print_exc()
        generateSummaries(stats=stats, experimentName="inc-rw")

    if (enabledExperiments[3]):

        try:
            # decreasing CProb, MProb with elitism selection but delta mutation func
            # params = GAParameters(MProb, CProb, totalEpochs, decreaseM = True, decreaseC = True, \
            # MStartEpoch = mStartEpoch, MEndEpoch = mEndEpoch, CStartEpoch = cStartEpoch, CEndEpoch = cEndEpoch, \
            # eliteRatio=eliteRatio)

            # constant CProb, MProb with elitism selection but delta mutation func
            params = GAParameters(MProb, CProb, totalEpochs, decreaseM = False, decreaseC = False, \
             MStartEpoch = mStartEpoch, MEndEpoch = mEndEpoch, CStartEpoch = cStartEpoch, CEndEpoch = cEndEpoch, \
             eliteRatio=eliteRatio)
            ga = Ga(r,
                    popSize,
                    registers,
                    generationFunction=QASMPrimitiveInstruction,
                    minSize=minSize,
                    maxSize=maxSize)
            stats = ga.evolve(totalEpochs, probCrossover = CProb, probMutation= MProb, \
                        evaluationFunc = incDecExp.evaluationFunction, selectionFunc= incDecExp.elitismSelection, \
                        mutationArgFunc = deltaMutationFunc, params = params)
        except Exception:
            traceback.print_exc()
        generateSummaries(stats=stats, experimentName="inc-e")
    input("Press [enter] to continue.")