# NETQASM 1.0
# APPID 0

# DEFINE pair_index R1
# DEFINE loop_index R6

# DEFINE num_repetitions 2
# DEFINE num_reps_times_bases 24

# DEFINE num_bases 12
# DEFINE num_bas_times_ten 120

# DEFINE epr_results_60 2
# DEFINE virt_ids_60 3

# DEFINE epr_results_65 4
# DEFINE virt_ids_65 5

# DEFINE array_index_jump 6

# DEFINE outcomes_60 0
# DEFINE outcomes_65 1

start:
    array $num_reps_times_bases @$outcomes_60
    array $num_reps_times_bases @$outcomes_65
    array $num_bas_times_ten @$epr_results_60
    array $num_bases @$virt_ids_60
    set R0 0
LOOP7:
    beq R0 $num_bases LOOP_EXIT7
    store 0 @$virt_ids_60[R0]
    add R0 R0 1
    jmp LOOP7
LOOP_EXIT7:
    array $num_bas_times_ten @$epr_results_65
    array $num_bases @$virt_ids_65
    set R0 0
LOOP8:
    beq R0 $num_bases LOOP_EXIT8
    store 0 @$virt_ids_65[R0]
    add R0 R0 1
    jmp LOOP8
LOOP_EXIT8:
    set $loop_index 0

main_loop_START:
    beq $loop_index $num_repetitions main_loop_END

    set C0 0
reset_results_array_START:
    beq C0 $num_bas_times_ten reset_results_array_END
    undef @$epr_results_60[C0]
    undef @$epr_results_65[C0]
    add C0 C0 1
    jmp reset_results_array_START
reset_results_array_END:

    recv_epr(0,0) $virt_ids_60 $epr_results_60

    set R1 0
LOOP2:
    beq R1 $num_bases LOOP_EXIT2
    set R2 0
    set R3 0
    set R4 0
    set R5 0
LOOP:
    beq R5 10 LOOP_EXIT
    add R2 R2 R1
    add R5 R5 1
    jmp LOOP
LOOP_EXIT:
    add R3 R1 1
    set R5 0
LOOP1:
    beq R5 10 LOOP_EXIT1
    add R4 R4 R3
    add R5 R5 1
    jmp LOOP1
LOOP_EXIT1:
    wait_all @$epr_results_60[R2:R4]
    load Q0 @$virt_ids_60[R1]

    beq $pair_index 0 rot_X_60
    beq $pair_index 1 rot_Y_60
    beq $pair_index 3 rot_minus_X_60
    beq $pair_index 4 rot_minus_Y_60
    beq $pair_index 5 rot_minus_Z_60
    beq $pair_index 6 rot_X_60
    beq $pair_index 7 rot_Y_60
    beq $pair_index 9 rot_minus_X_60
    beq $pair_index 10 rot_minus_Y_60
    beq $pair_index 11 rot_minus_Z_60
    jmp rot_END_60

rot_X_60:
    rot_y Q0 3 1
    jmp rot_END_60
rot_Y_60:
    rot_x Q0 1 1
    jmp rot_END_60
rot_minus_X_60:
    rot_y Q0 1 1
    jmp rot_END_60
rot_minus_Y_60:
    rot_x Q0 3 1
    jmp rot_END_60
rot_minus_Z_60:
    rot_x Q0 1 0
    jmp rot_END_60
rot_END_60:

    meas Q0 M0
    qfree Q0

    set C0 0
    set C1 0
    add C1 C1 $pair_index
calc_index_60_START:
    beq C0 $loop_index calc_index_60_END
    add C1 C1 $num_bases
    add C0 C0 1
    jmp calc_index_60_START
calc_index_60_END:

    store M0 @$outcomes_60[C1]
    add R1 R1 1
    jmp LOOP2
LOOP_EXIT2:
    recv_epr(0,0) $virt_ids_65 $epr_results_65
    set R1 0
LOOP5:
    beq R1 $num_bases LOOP_EXIT5
    set R2 0
    set R3 0
    set R4 0
    set R5 0
LOOP3:
    beq R5 10 LOOP_EXIT3
    add R2 R2 R1
    add R5 R5 1
    jmp LOOP3
LOOP_EXIT3:
    add R3 R1 1
    set R5 0
LOOP4:
    beq R5 10 LOOP_EXIT4
    add R4 R4 R3
    add R5 R5 1
    jmp LOOP4
LOOP_EXIT4:
    wait_all @$epr_results_65[R2:R4]
    load Q0 @$virt_ids_65[R1]

    beq $pair_index 0 rot_X_65
    beq $pair_index 1 rot_Y_65
    beq $pair_index 3 rot_minus_X_65
    beq $pair_index 4 rot_minus_Y_65
    beq $pair_index 5 rot_minus_Z_65
    beq $pair_index 6 rot_X_65
    beq $pair_index 7 rot_minus_Y_65
    beq $pair_index 9 rot_minus_X_65
    beq $pair_index 10 rot_minus_Y_65
    beq $pair_index 11 rot_minus_Z_65
    jmp rot_END_65

rot_X_65:
    rot_y Q0 3 1
    jmp rot_END_65
rot_Y_65:
    rot_x Q0 1 1
    jmp rot_END_65
rot_minus_X_65:
    rot_y Q0 1 1
    jmp rot_END_65
rot_minus_Y_65:
    rot_x Q0 3 1
    jmp rot_END_65
rot_minus_Z_65:
    rot_x Q0 1 0
    jmp rot_END_65
rot_END_65:

    meas Q0 M0
    qfree Q0

    set C0 0
    set C1 0
    add C1 C1 $pair_index
calc_index_65_START:
    beq C0 $loop_index calc_index_65_END
    add C1 C1 $num_bases
    add C0 C0 1
    jmp calc_index_65_START
calc_index_65_END:

    store M0 @$outcomes_65[C1]
    add R1 R1 1
    jmp LOOP5
LOOP_EXIT5:

    add $loop_index $loop_index 1
    jmp main_loop_START
main_loop_END:

    ret_arr @$outcomes_60
    ret_arr @$outcomes_65
    ret_arr @$epr_results_60
    ret_arr @$virt_ids_60
    ret_arr @$epr_results_65
    ret_arr @$virt_ids_65