from qiskit import QuantumRegister, QuantumCircuit, execute, Aer


cq = QuantumRegister(2,'code\_qubit')
lq = QuantumRegister(1,'link\_qubit')
qc = QuantumCircuit(cq,lq)
qc.cx(cq[0],lq[0])
qc.cx(cq[1],lq[0])
print(qc)
qc.draw('mpl')

from qiskit.ignis.verification.topological_codes import RepetitionCode
from qiskit.ignis.verification.topological_codes import GraphDecoder
from qiskit.ignis.verification.topological_codes import lookuptable_decoding, postselection_decoding

d = 2
T = 1
code = RepetitionCode(d,T)

code.circuit

print ( 'code_bits:', code.code_bit, ' d=', code.d, ' T=', code.T)

code.qubit_registers

code.code_qubit

for bit in ['0','1']:
    print('\n========= logical',bit,'=========\n')
    print( code.circuit[bit] )

d = 2
T = 1
code = RepetitionCode(d,T)

for bit in ['0','1']:
    print('\n========= logical',bit,'=========\n')
    print( code.circuit[bit] )

empty_code = RepetitionCode(3,0)

def print_circuits(code):
    for log in ['0','1']:
        print('\n========= logical',log,'=========\n')
        print( code.circuit[log] )
            
print_circuits(empty_code)

empty_code.syndrome_measurement()
print_circuits(empty_code)

empty_code.x()
print_circuits(empty_code)

empty_code.readout()
print_circuits(empty_code)

circuits = code.get_circuit_list()
job = execute( circuits, Aer.get_backend('qasm_simulator') )
raw_results = {}
for log in ['0','1']:
    raw_results[log] = job.result().get_counts(log)
    print('\n========= logical',log,'=========\n')
    print(raw_results[log])








