import warnings

from loguru import logger
from qiskit import QuantumCircuit, transpile
from qiskit_aer import AerSimulator

from qiskit_class_converter import ConversionService, ConversionType, __FULL_VERSION__

warnings.filterwarnings('ignore')

__FULL_VERSION__

input_value = [
    [1, 0, 0, 0],
    [0, 0, 0, 1],
    [0, 0, 1, 0],
    [0, 1, 0, 0]
]
sample_converter = ConversionService(conversion_type="MATRIX_TO_QC", option={"label": "CX gate"})
result = sample_converter.convert(input_value=input_value)
quantum_circuit = QuantumCircuit(2, 2)
quantum_circuit.x(0)
quantum_circuit.append(result, [0, 1])
quantum_circuit.measure(range(2), range(2))
backend = AerSimulator()
qc_compiled = transpile(quantum_circuit, backend)
logger.info("\n" + str(quantum_circuit))

quantum_circuit = QuantumCircuit(2, 2)
quantum_circuit.x(0)
quantum_circuit.cx(0, 1)
sample_converter = ConversionService(conversion_type="QC_TO_MATRIX")
result = sample_converter.convert(input_value=quantum_circuit)
for gate in result["gate"]:
    logger.info("\n" + str(gate.astype(int)))
logger.info("list: " + str(result["name"]))
result["result"].astype(int)

# quantum circuit to matrix (for print: raw option)
quantum_circuit = QuantumCircuit(2, 2)
quantum_circuit.x(0)
quantum_circuit.cx(0, 1)
sample_converter = ConversionService(conversion_type="QC_TO_MATRIX", option={"print": "raw"})
result = sample_converter.convert(input_value=quantum_circuit)
for gate in result["gate"]:
    logger.info(gate)

logger.info(result["result"])
from IPython.display import Latex
Latex(result["result"])

quantum_circuit = QuantumCircuit(2, 2)
quantum_circuit.h(0)
quantum_circuit.x(0)
quantum_circuit.cx(0, 1)
sample_converter = ConversionService(conversion_type="QC_TO_BRA_KET")
result = sample_converter.convert(input_value=quantum_circuit)
result

quantum_circuit = QuantumCircuit(2, 2)
quantum_circuit.h(0)
quantum_circuit.x(0)
quantum_circuit.cx(0, 1)
sample_converter = ConversionService(conversion_type="QC_TO_BRA_KET", option={"print": "raw"})
result = sample_converter.convert(input_value=quantum_circuit)

logger.info(result)
from IPython.display import Math
Math(result)

sample_converter = ConversionService(conversion_type="STR_TO_BRA_KET")
result = sample_converter.convert(input_value="sqrt(2)*|00>/2+sqrt(2)*|11>/2")
logger.info(result)
logger.info(type(result))

sample_converter = ConversionService(conversion_type="STR_TO_BRA_KET", option={"print": "raw"})
result = sample_converter.convert(input_value="sqrt(2)*|00>/2+sqrt(2)*|11>/2")

logger.info(result)
from IPython.display import Math
Math(result)