# (O_g \otimes I)
for gate in Og:
    line = gate + halfn*"_"
    circuit += line + "\n"
# (\prod^{n/2}_{i=1} CZ_{i, i+n/2})
for i in range(halfn):
    line = i*"_" + "C" + "_"*(halfn - 1) + "Z" + "_"*(halfn-i-1)
    circuit += line + "\n"

# Hadamard all qubits
for i in range(n):
    line = "_"*i + "H" + "_"*(n-1-i)
    circuit += line + "\n"

# compile
compiled = compileCircuit(raw=circuit)

# ####### INFORMATION ######## #
# Show circuit
if (printCirc):
    print("Circuit:")
    print(circuit)

print("Hidden shift string:")
print("".join(list(s.astype(str))))

print("%d hidden shift bits, %d qubits, %d T gates, %d samples, k=%d" %
      (n, len(compiled.splitlines()[0]), toff*8, samples, k))

# ####### RUN ALGORITHM ######## #
Esempio n. 2
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))