# Path: libcirc/compile/compilecirc.py
# def standardGate(line, listMode=False, lineMode=False):
#     if line == "main":
#         if listMode or lineMode:
#             raise ValueError("Can't bring 'main' into list form.")
#         return "main"
# 
#     if not lineMode:
#         toRemove = ["_", "M"]
#         for r in toRemove:
#             line = line.replace(r, "")
# 
#     elems = []
#     for letter in line:
#         if not letter.islower(): elems.append(letter)
#         else: elems = elems[:-1] + [elems[-1] + letter]
# 
#     if lineMode: return elems
#     if listMode: return sorted(elems)
#     return "".join(sorted(elems))


from libcirc.compile.compilecirc import standardGate
import numpy as np
        for Midx in Ms:
            if gate[Midx] not in ['M', '_']:
                raise SyntaxError("Line %d: Cannot perform operations to measured qubit %d" % (lineNr, Midx))

        if 'T' in gate and 'M' not in gate:
            Ts += 1  # count T gate

            # Put qubit into Tcache
            Tidx = gate.index('T')
            if Tidx in Tcache:  # already there: consecutive Ts
                raise SyntaxError("Line %d: Consecutive Ts on qubit %d" % (lineNr, Midx))
            Tcache.append(Tidx)

        # remove qubits from Tcache if a gate is encountered
        for Tidx in Tcache:
            if gate[Tidx] != '_':
                Tcache.remove(Tidx)

        # T that is applied conditionally on a measurement
        if 'T' in gate and 'M' in gate:
            Midx = gate.index('M')
            MTs.append(Midx)

    return Ts, Ms, MTs


# contract circuit into single projector
# Mdict: postselected bits to determine circuit
# y: postselected T ancilla measurements
def gadgetize(circuit, Mdict, y):

# Path: libcirc/compile/compilecirc.py
# def standardGate(line, listMode=False, lineMode=False):
#     if line == "main":
#         if listMode or lineMode:
#             raise ValueError("Can't bring 'main' into list form.")
#         return "main"
# 
#     if not lineMode:
#         toRemove = ["_", "M"]
#         for r in toRemove:
#             line = line.replace(r, "")
# 
#     elems = []
#     for letter in line:
#         if not letter.islower(): elems.append(letter)
#         else: elems = elems[:-1] + [elems[-1] + letter]
# 
#     if lineMode: return elems
#     if listMode: return sorted(elems)
#     return "".join(sorted(elems))


# Path: libcirc/compile/gadgetize.py
from libcirc.compile.compilecirc import standardGate
import numpy as np

        for Midx in Ms:
            if gate[Midx] not in ['M', '_']:
                raise SyntaxError("Line %d: Cannot perform operations to measured qubit %d" % (lineNr, Midx))

        if 'T' in gate and 'M' not in gate:
            Ts += 1  # count T gate

            # Put qubit into Tcache
            Tidx = gate.index('T')
            if Tidx in Tcache:  # already there: consecutive Ts
                raise SyntaxError("Line %d: Consecutive Ts on qubit %d" % (lineNr, Midx))
            Tcache.append(Tidx)

        # remove qubits from Tcache if a gate is encountered
        for Tidx in Tcache:
            if gate[Tidx] != '_':
                Tcache.remove(Tidx)

        # T that is applied conditionally on a measurement
        if 'T' in gate and 'M' in gate:
            Midx = gate.index('M')
            MTs.append(Midx)

    return Ts, Ms, MTs


# contract circuit into single projector
# Mdict: postselected bits to determine circuit
# y: postselected T ancilla measurements
def gadgetize(circuit, Mdict, y):

    n = len(standardGate(circuit.split('\n', 1)[0], lineMode=True))