def product_pauli_z(q1, q2, coeff, n_q): eye = np.eye(n_q) return operators([[coeff, Pauli(eye[q1], np.zeros(n_q)) * Pauli(eye[q2], np.zeros(n_q))]])
def pauli_z(qubit, coeff, n_q): eye = np.eye(n_q) return operators([[coeff, Pauli(eye[qubit], np.zeros(n_q))]])
def pauli_i(coeff, n_q): id_pauli = Pauli(np.zeros(n_q), np.zeros(n_q)) return operators([[coeff, id_pauli]])
def pauli_x(qubit, coeff, n_q): eye = np.eye(n_q) return operators([[coeff, Pauli(np.zeros(n_q), eye[qubit])]])
def get_tsp_qubitops(ins, penalty=1e5): """Generate Hamiltonian for TSP of a graph. Args: ins (TspData) : TSP data including coordinates and distances. penalty (float) : Penalty coefficient for the constraints Returns: operator.Operator, float: operator for the Hamiltonian and a constant shift for the obj function. """ num_nodes = ins.dim num_qubits = num_nodes**2 zero = np.zeros(num_qubits, dtype=np.bool) pauli_list = [] shift = 0 for i in range(num_nodes): for j in range(num_nodes): if i == j: continue for p in range(num_nodes): q = (p + 1) % num_nodes shift += ins.w[i, j] / 4 zp = np.zeros(num_qubits, dtype=np.bool) zp[i * num_nodes + p] = True pauli_list.append([-ins.w[i, j] / 4, Pauli(zp, zero)]) zp = np.zeros(num_qubits, dtype=np.bool) zp[j * num_nodes + q] = True pauli_list.append([-ins.w[i, j] / 4, Pauli(zp, zero)]) zp = np.zeros(num_qubits, dtype=np.bool) zp[i * num_nodes + p] = True zp[j * num_nodes + q] = True pauli_list.append([ins.w[i, j] / 4, Pauli(zp, zero)]) for i in range(num_nodes): for p in range(num_nodes): zp = np.zeros(num_qubits, dtype=np.bool) zp[i * num_nodes + p] = True pauli_list.append([penalty, Pauli(zp, zero)]) shift += -penalty for p in range(num_nodes): for i in range(num_nodes): for j in range(i): shift += penalty / 2 zp = np.zeros(num_qubits, dtype=np.bool) zp[i * num_nodes + p] = True pauli_list.append([-penalty / 2, Pauli(zp, zero)]) zp = np.zeros(num_qubits, dtype=np.bool) zp[j * num_nodes + p] = True pauli_list.append([-penalty / 2, Pauli(zp, zero)]) zp = np.zeros(num_qubits, dtype=np.bool) zp[i * num_nodes + p] = True zp[j * num_nodes + p] = True pauli_list.append([penalty / 2, Pauli(zp, zero)]) for i in range(num_nodes): for p in range(num_nodes): for q in range(p): shift += penalty / 2 zp = np.zeros(num_qubits, dtype=np.bool) zp[i * num_nodes + p] = True pauli_list.append([-penalty / 2, Pauli(zp, zero)]) zp = np.zeros(num_qubits, dtype=np.bool) zp[i * num_nodes + q] = True pauli_list.append([-penalty / 2, Pauli(zp, zero)]) zp = np.zeros(num_qubits, dtype=np.bool) zp[i * num_nodes + p] = True zp[i * num_nodes + q] = True pauli_list.append([penalty / 2, Pauli(zp, zero)]) shift += 2 * penalty * num_nodes return operators(paulis=pauli_list), shift