Exemplo n.º 1
0
 def prob(measure, exact=None):  # shortcut
     if exact is not None:
         old = config["exact"]
         config["exact"] = exact
     P = probability(circ, measure, config=config)
     if exact is not None: config["exact"] = old
     return P
Exemplo n.º 2
0
    # calculate expected output
    Tgate = np.array([[1+0j, 0+0j], [0+0j, np.exp(1j*np.pi/4)]])
    Hgate = np.array([[1+0j, 1+0j], [1+0j, -1+0j]])/np.sqrt(2)

    # State = np.dot(Hgate, np.array([1+0j, 0+0j]))
    # for i in range(T):
    #     State = np.dot(Tgate, State)
    #     State = np.dot(Hgate, State)
    # target = np.abs(State[0])**2

    # prepare circuit
    circ = "H\n"
    for i in range(T): circ += "T\nH\n"

    (tnum, tdenom) = probability(circ, {0:0}, config={"python": True, "quiet":True, "noapprox":True, "direct":True})
    tnum = L*tnum
    tdenom = L*tdenom

    if verbose: print("Target outputs:", tnum, tdenom)

    # multiplicative error
    enum = np.sqrt((2**T - 1)/(2**T + 1))*tnum/np.sqrt(L*pf)
    edenom = np.sqrt((2**T - 1)/(2**T + 1))*tdenom/np.sqrt(L*pf)
    data["num"]["targeterr"].append(enum)
    data["denom"]["targeterr"].append(edenom)

    if False:
        if verbose: print("L sampling")
        fidbound = 1e-3
        config = {"python":python, "fidbound":fidbound, "exact":False,
Exemplo n.º 3
0
# ####### RUN ALGORITHM ######## #

config = {
    "procs": 8,
    "samples": samples,
    "k": k,
    "exact": False,
    "rank": True,
}

starttime = datetime.now()

results = []
for i in range(n):
    measure = {i: 1}
    result = probability(compiled, measure, config=config)

    print("Bit %d: %f" % (i, result))
    results.append(result)

print("Time elapsed: " + str(datetime.now() - starttime))

if plot:
    import matplotlib.pyplot as plt
    plt.bar(range(n), results, 0.9, align="center")
    plt.title("Hidden shift algorithm output")
    plt.xticks(range(n), s)
    plt.ylabel("Probability")
    plt.xlabel("Hidden shift bit")
    plt.show()
Exemplo n.º 4
0
def main(argv):
    if len(argv) > 1 and argv[1] == "-h":
        return help()

    if len(argv) < 3:
        return usage("Wrong number of arguments.")

    samples = None
    sampleerror = None
    config = {}

    mpiarg = False

    # parse optional arguments
    for i in range(3, len(argv)):
        if mpiarg:
            if argv[i][-1] == '"':
                mpiarg = False
                config["mpirun"] += " " + argv[i][:-1]
            else:
                config["mpirun"] += " " + argv[i]

        # Logging
        elif argv[i] == "-v":
            config["verbose"] = True
        elif argv[i] == "-sp":
            config["silenceprojectors"] = True
        elif argv[i] == "-quiet":
            config["quiet"] = True

            # Sampling
        elif argv[i] == "-noapprox":
            config["noapprox"] = True
        elif argv[i][:8] == "samples=":
            config["samples"] = int(float(argv[i][8:]))
        elif argv[i][:5] == "bins=":
            config["bins"] = int(float(argv[i][5:]))
        elif argv[i][:6] == "error=":
            config["error"] = float(argv[i][6:])
        elif argv[i][:9] == "failprob=":
            config["failprob"] = float(argv[i][9:])

            # State preparation
        elif argv[i] == "-exact":
            config["exact"] = True
        elif argv[i][:2] == "k=":
            config["k"] = int(float(argv[i][2:]))
        elif argv[i][:9] == "fidbound=":
            config["fidbound"] = float(argv[i][9:])
        elif argv[i] == "-fidelity":
            config["fidelity"] = True
        elif argv[i] == "-rank":
            config["rank"] = True

            # Backend
        elif argv[i] == "-py":
            config["python"] = True
        elif argv[i][:6] == "cpath=":
            config["cpath"] = argv[i][6:]
        elif argv[i][:8] == 'mpirun="':
            if argv[i][-1] == '"':
                config["mpirun"] = argv[i][8:-1]
            else:
                mpiarg = True
                config["mpirun"] = argv[i][8:]
        elif argv[i][:6] == "procs=":
            config["procs"] = int(argv[i][6:])
        elif argv[i][:5] == "file=":
            config["file"] = argv[i][5:]

            # Debug
        elif argv[i][:2] == "y=":
            config["y"] = argv[i][2:]
        elif argv[i][:2] == "x=":
            config["x"] = argv[i][2:]
        elif argv[i] == "-forceL":
            config["forceL"] = True
        else:
            raise ValueError("Invalid argument: " + argv[i])

    if config.get("verbose"):
        if config.get("quiet"):
            config["quiet"] = False
            print("Warning: -v option overrides -quiet.")

    quiet = config.get("quiet")

    if config.get("error") is not None or config.get("failprob") is not None:
        if config.get("samples") is not None:
            config["samples"] = None
            if not quiet:
                print(
                    "Warning: error and failprob options override samples option."
                )
        if config.get("bins") is not None:
            config["bins"] = None
            if not quiet:
                print(
                    "Warning: error and failprob options override bins option."
                )

    if config.get("exact"):
        if config.get("k") is not None:
            config["k"] = None
            if not quiet: print("Warning: -exact option overrides k option.")
        if config.get("fidbound") is not None:
            config["fidbound"] = None
            if not quiet:
                print("Warning: -exact option overrides fidbound option.")

    if config.get("k") is not None:
        config["exact"] = False
        if config.get("fidbound") is not None:
            if not quiet: print("Warning: k option overrides fidbound option.")
            config["fidbound"] = None

    if config.get("fidbound") is not None:
        config["exact"] = False

    if not re.match("^(1|0|M|_)*$", argv[2]):
        return usage(
            "Measurement string must consists only of '1', '0', 'M' or '_'.")

    # load input circuit
    infile = argv[1]
    circ = compilecirc.compileCircuit(fname=infile)

    # get measurements
    measure = {}
    sample = []
    for i in range(len(argv[2])):
        if list(argv[2])[i] == '0':
            measure[i] = 0
        if list(argv[2])[i] == '1':
            measure[i] = 1
        if list(argv[2])[i] == 'M':
            sample.append(i)

    # start timer
    if config.get("verbose"): starttime = datetime.now()

    if len(sample) == 0:  # algorithm 1: compute probability
        if config.get("verbose"):
            print(
                "Probability mode: calculate probability of measurement outcome"
            )

        if config.get("exact") is not None and config.get("exact") is False:
            if not quiet:
                print(
                    "Warning: Probability calculation requires exact sampling rather than L sampling.\nWill use |L> state anyway, as requested."
                )

        P = probability(circ, measure, config=config)
        if P is None:
            print("Program gave no output probability")
        else:
            print("Probability: " + str(P))

    else:  # algorithm 2: sample bits
        if config.get("verbose"): print("Sample mode: sample marked bits")

        X = sampleQubits(circ, measure, sample, config=config)
        if len(X) == 0:  # no sample possible
            print(
                "Error: circuit output state cannot produce a measurement with these constraints."
            )
        else:
            print("Sample: " + str(X))

    if config.get("verbose"):
        print("Time elapsed: " + str(datetime.now() - starttime))
Exemplo n.º 5
0
    "exact": False,
    "rank": False,
    "fidelity": True,
    "y": None,
    "x": None,
    "python": False,
    "cpath": "libcirc/sample",
}


starttime = datetime.now()

results = []
for i in range(n):
    measure = {i: 1}
    result = probability(compiled, measure, config)

    print("Bit %d: %f" % (i, result))
    results.append(result)

print("Time elapsed: " + str(datetime.now() - starttime))

if plot:
    import matplotlib.pyplot as plt
    plt.bar(range(n), results, 0.9, align="center")
    plt.title("Hidden shift algorithm output")
    plt.xticks(range(n), s)
    plt.ylabel("Probability")
    plt.xlabel("Hidden shift bit")
    plt.show()
Exemplo n.º 6
0
            circ += "T\nH\n"

        times = []
        outs = []
        for i in range(ntests):
            if (i > 0):
                print("Test: ",
                      i + 1,
                      "/",
                      ntests,
                      "(remaining",
                      np.round((ntests - i) * np.mean(times) / 60, 2),
                      " min)",
                      end="\r")
            starttime = datetime.now()
            out = probability(circ, {0: 0}, config=config)
            dt = (datetime.now() - starttime)
            times.append(dt.seconds + dt.microseconds / 1e6)
            outs.append(out)

        data = {
            "k": k,
            "t": t,
            "mode": mode,
            "L": L,
            "ntests": ntests,
            "mutime": np.mean(times),
            "sigmatime": np.std(times),
            "muout": np.mean(outs),
            "sigmaout": np.std(outs)
        }
Exemplo n.º 7
0
def main(argv):
    if len(argv) > 1 and argv[1] == "-h":
        return help()

    if len(argv) == 1:
        return usage("Wrong number of arguments.")

    # built-in test script
    HTstack = -1
    if argv[1][:3] == "HT=":
        HTstack = int(argv[1][3:])

    if len(argv) < 3 and HTstack == -1:
        return usage("Wrong number of arguments.")

    config = {}

    mpiarg = False

    # parse optional arguments
    for i in range(3 if HTstack == -1 else 2, len(argv)):
        if mpiarg:
            if argv[i][-1] == '"':
                mpiarg = False
                config["mpirun"] += " " + argv[i][:-1]
            else:
                config["mpirun"] += " " + argv[i]

        # HT stack error message
        elif argv[i][:3] == "HT=":
            return usage("HT=? option ignored. Put instead of input file.")

            # Logging
        elif argv[i] == "-v":
            config["verbose"] = True
        elif argv[i] == "-sp":
            config["silenceprojectors"] = True
        elif argv[i] == "-quiet":
            config["quiet"] = True

            # Sampling
        elif argv[i] == "-nosampling":
            config["noapprox"] = True
        elif argv[i][:8] == "samples=":
            config["samples"] = int(float(argv[i][8:]))
        elif argv[i][:5] == "bins=":
            config["bins"] = int(float(argv[i][5:]))
        elif argv[i][:6] == "error=":
            config["error"] = float(argv[i][6:])
        elif argv[i][:9] == "failprob=":
            config["failprob"] = float(argv[i][9:])

            # State preparation
        elif argv[i] == "-exactstate":
            config["exact"] = True
        elif argv[i][:2] == "k=":
            config["k"] = int(float(argv[i][2:]))
        elif argv[i][:9] == "fidbound=":
            config["fidbound"] = float(argv[i][9:])
        elif argv[i] == "-fidelity":
            config["fidelity"] = True
        elif argv[i] == "-rank":
            config["rank"] = True

            # Backend
        elif argv[i] == "-py":
            config["python"] = True
        elif argv[i][:6] == "cpath=":
            config["cpath"] = argv[i][6:]
        elif argv[i][:8] == 'mpirun="':
            if argv[i][-1] == '"':
                config["mpirun"] = argv[i][8:-1]
            else:
                mpiarg = True
                config["mpirun"] = argv[i][8:]
        elif argv[i][:6] == "procs=":
            config["procs"] = int(argv[i][6:])
        elif argv[i][:5] == "file=":
            config["file"] = argv[i][5:]

            # Debug
        elif argv[i][:2] == "y=":
            config["y"] = argv[i][2:]
        elif argv[i][:2] == "x=":
            config["x"] = argv[i][2:]
        elif argv[i] == "-forceL":
            config["forceL"] = True
        elif i == 2:
            return usage("HT=? option replaces file and measurement argument.")
        else:
            return usage("Invalid argument: " + argv[i])

    if config.get("verbose"):
        if config.get("quiet"):
            config["quiet"] = False
            print("Warning: -v option overrides -quiet.")

    quiet = config.get("quiet")

    if config.get("error") is not None or config.get("failprob") is not None:
        if config.get("samples") is not None:
            config["samples"] = None
            if not quiet:
                print(
                    "Warning: error and failprob options override samples option."
                )
        if config.get("bins") is not None:
            config["bins"] = None
            if not quiet:
                print(
                    "Warning: error and failprob options override bins option."
                )

    if config.get("exact"):
        if config.get("k") is not None:
            config["k"] = None
            if not quiet: print("Warning: -exact option overrides k option.")
        if config.get("fidbound") is not None:
            config["fidbound"] = None
            if not quiet:
                print("Warning: -exact option overrides fidbound option.")

    if config.get("k") is not None:
        config["exact"] = False
        if config.get("fidbound") is not None:
            if not quiet: print("Warning: k option overrides fidbound option.")
            config["fidbound"] = None

    if config.get("fidbound") is not None:
        config["exact"] = False

    if config.get("forceL") is not None:
        config["exact"] = False

    if HTstack == -1:
        if not re.match("^(1|0|M|_)*$", argv[2]):
            return usage(
                "Measurement string must consists only of '1', '0', 'M' or '_'."
            )

        # load input circuit
        infile = argv[1]
        circ = compilecirc.compileCircuit(fname=infile)

        # get measurements
        measure = {}
        sample = []
        for i in range(len(argv[2])):
            if list(argv[2])[i] == '0':
                measure[i] = 0
            if list(argv[2])[i] == '1':
                measure[i] = 1
            if list(argv[2])[i] == 'M':
                sample.append(i)
    else:
        circ = ""
        for i in range(HTstack):
            circ += "H\n"
            circ += "T\n"
        circ += "H\n"

        measure = {0: 0}
        sample = []

        if config.get("y") is not None:
            if not quiet: print("Warning: override y to all zero string.")

        config["y"] = "0" * HTstack

    # start timer
    if config.get("verbose"): starttime = datetime.now()

    if len(sample) == 0:  # algorithm 1: compute probability
        if config.get("verbose"):
            print(
                "Probability mode: calculate probability of measurement outcome"
            )

        if config.get("exact") is not None and config.get("exact") is False:
            if not quiet:
                print(
                    "Warning: Probability calculation requires exact sampling rather than L sampling.\nWill use |L> state anyway, as requested."
                )

        P = probability(circ, measure, config=config)
        if P is None:
            print("Program gave no output probability")
        else:
            print("Probability: " + str(P))

    else:  # algorithm 2: sample bits
        if config.get("verbose"): print("Sample mode: sample marked bits")

        X = sampleQubits(circ, measure, sample, config=config)
        if len(X) == 0:  # no sample possible
            print(
                "Error: circuit output state cannot produce a measurement with these constraints."
            )
        else:
            print("Sample: " + str(X))

    if config.get("verbose"):
        print("Time elapsed: " + str(datetime.now() - starttime))

    # calculate reference value for HT stack
    if HTstack != -1 and P is not None:
        import numpy as np
        H = np.array([[1, 1], [1, -1]]) / np.sqrt(2)
        T = np.array([[1, 0], [0, np.exp(1j * np.pi / 4)]])

        out = H
        for i in range(HTstack):
            out = np.dot(out, T)
            out = np.dot(out, H)

        Pref = np.abs(out[0, 0])**2
        print("HT stack reference value: " + str(Pref))
        print("Error: " + str(np.abs(P - Pref)))
Exemplo n.º 8
0
def main(argv):
    if len(argv) > 1 and argv[1] == "-h":
        return help()

    if len(argv) < 3:
        return usage("Wrong number of arguments.")

    config = {
        "verbose": False,
        "silenceprojectors": False,
        "parallel": True,
        "samples": int(1e3),
        "fidbound": 1e-5,
        "k": None,
        "exact": False,
        "rank": False,
        "fidelity": False,
        "y": None,
        "x": None,
        "python": False,
        "cpath": "libcirc/sample",
    }

    # parse optional arguments
    for i in range(3, len(argv)):

        if argv[i] == "-v": config["verbose"] = True
        elif argv[i] == "-np": config["parallel"] = False
        elif argv[i][:8] == "samples=": config["samples"] = int(float(argv[i][8:]))
        elif argv[i][:9] == "fidbound=": config["fidbound"] = float(argv[i][9:])
        elif argv[i][:2] == "k=": config["k"] = int(float(argv[i][2:]))
        elif argv[i][:2] == "y=": config["y"] = argv[i][2:]
        elif argv[i][:2] == "x=": config["x"] = argv[i][2:]
        elif argv[i] == "-exact": config["exact"] = True
        elif argv[i] == "-rank": config["rank"] = True
        elif argv[i] == "-fidelity": config["fidelity"] = True
        elif argv[i] == "-py": config["python"] = True
        elif argv[i] == "-sp": config["silenceprojectors"] = True
        elif argv[i][:6] == "cpath=": config["cpath"] = argv[i][6:]
        else: raise ValueError("Invalid argument: " + argv[i])

    if not re.match("^(1|0|M|_)*$", argv[2]):
        return usage("Measurement string must consists only of '1', '0', 'M' or '_'.")

    # load input circuit
    infile = argv[1]
    circ = compilecirc.compileCircuit(fname=infile)

    # get measurements
    measure = {}
    sample = []
    for i in range(len(argv[2])):
        if list(argv[2])[i] == '0':
            measure[i] = 0
        if list(argv[2])[i] == '1':
            measure[i] = 1
        if list(argv[2])[i] == 'M':
            sample.append(i)

    # start timer
    if config["verbose"]: starttime = datetime.now()

    if len(sample) == 0:  # algorithm 1: compute probability
        if config["verbose"]: print("Probability mode: calculate probability of measurement outcome")
        config["exact"] = True

        P = probability(circ, measure, config)
        print("Probability:", P)

    else:  # algorithm 2: sample bits
        if config["verbose"]: print("Sample mode: sample marked bits")

        X = sampleQubits(circ, measure, sample, config)
        if len(X) == 0:  # no sample possible
            print("Error: circuit output state cannot produce a measurement with these constraints.")
        else:
            print("Sample:", X)

    if config["verbose"]:
        print("Time elapsed: ", str(datetime.now() - starttime))