# AUTOGENERATED! DO NOT EDIT! File to edit: ../../src/inference/infer_misc.ipynb.

# %% auto 0
__all__ = ['get_rnd_gatepool_subset', 'convert_tensors_to_circuits']

# %% ../../src/inference/infer_misc.ipynb 2
from ..imports import *
from ..platform.qcircuit_dataset_construction import decode_circuit, gate_pool_to_gate_classes
from ..platform.simulation.qcircuit_sim import instruction_name_to_qiskit_gate

# %% ../../src/inference/infer_misc.ipynb 4
def get_rnd_gatepool_subset(gate_pool, min_sub_gate_pool_cnt=2):
    rng = np.random.default_rng()
    
    n       = len(gate_pool) + 1
    c_range = np.arange(n-1)
    
    sub_gate_pool_cnt = rng.integers(min_sub_gate_pool_cnt, n)       
    sub_gate_pool_ind = rng.choice(c_range, size=sub_gate_pool_cnt, replace=False)         
    sub_gate_pool     = [gate_pool[ind] for ind in sub_gate_pool_ind]  # pick random subeset of gates
        
    return sub_gate_pool

# %% ../../src/inference/infer_misc.ipynb 6
def convert_tensors_to_circuits(out_tensor, gate_pool, params_tensor=None, place_barrier=False): 
    if isinstance(gate_pool[0], str):
        gate_pool = [instruction_name_to_qiskit_gate(gate) for gate in gate_pool]
    
    error_cnt = 0
    qc_list   = []

    if not exists(params_tensor):
        params_tensor = [None]*out_tensor.shape[0]
    
    #TODO: para this loop
   
    for i,(enc_tensor,p) in enumerate(zip(out_tensor, params_tensor)):   
        try:
            qc = decode_circuit(enc_tensor=enc_tensor, gate_pool=gate_pool, place_barrier=place_barrier, params_tensor=p)

        except Exception as e:
            error_cnt += 1
            # print(e)
            continue
                 
        qc_list.append(qc)  
  
    return qc_list, error_cnt
