def build_alternating_cnots_circuit(n_qubits, n_layers, params, skips=None):
    if skips is None:
        skips = []
    c = Circuit(n_qubits)
    cnot_count = 0
    cnots_full = 0
    for i in range(n_layers):
        if i % 2:
            for q in range(n_qubits // 2):
                if cnots_full not in skips:
                    c.add_operation(OpType.U3, params[2 * cnot_count], [2 * q])
                    c.add_operation(OpType.U3, params[2 * cnot_count + 1],
                                    [2 * q + 1])
                    c.CX(2 * q, 2 * q + 1)
                    cnot_count += 1
                cnots_full += 1
        else:
            for q in range((n_qubits - 1) // 2):
                if cnots_full not in skips:
                    c.add_operation(OpType.U3, params[2 * cnot_count],
                                    [2 * q + 1])
                    c.add_operation(OpType.U3, params[2 * cnot_count + 1],
                                    [2 * q + 2])
                    c.CX(2 * q + 1, 2 * q + 2)
                    cnot_count += 1
                cnots_full += 1
    for q in range(n_qubits):
        c.add_operation(OpType.U3, params[-q], [q])
    return c
def instructions_to_circuit(instructions):
    c = Circuit(n_qubit)
    for inst in instructions:
        t = inst.op.get_type()
        c.add_operation(t, inst.op.get_params(), inst.qubits)
    return c


# circ = instructions_to_circuit(circ.get_commands()[:24])

# print(circ.n_gates)

# print(dag_to_circuit(tk_to_dagcircuit(circ)))
def add_params_to_template(template: Circuit, params: np.ndarray):
    params = params.reshape((template.n_gates * 2 + template.n_qubits, 3))
    c = Circuit(template.n_qubits)
    for i in range(template.n_gates):
        cnot_gate = template.get_commands()[i]
        c.add_operation(OpType.U3, params[2 * i], [cnot_gate.qubits[0]])
        c.add_operation(OpType.U3, params[2 * i + 1], [cnot_gate.qubits[1]])
        c.CX(cnot_gate.qubits[0], cnot_gate.qubits[1])
    for q in range(template.n_qubits):
        c.add_operation(OpType.U3, params[template.n_gates * 2 + q], [q])
    return c
Example #4
0
 def instructions_to_circuit(self, instructions):
     c = Circuit(self.n_qubits, self.n_qubits)
     for inst in instructions:
         t = inst.op.get_type()
         c.add_operation(t, inst.op.get_params(), inst.qubits)
     return c