def qtest(angle): state = QuantumState(3) state.set_Haar_random_state() circuit = QuantumCircuit(3) circuit.add_X_gate(0) merged_gate = merge(CNOT(0, 1), Y(1)) circuit.add_gate(merged_gate) circuit.add_RX_gate(1, angle) circuit.update_quantum_state(state) observable = Observable(3) observable.add_operator(2.0, "X 2 Y 1 Z 0") observable.add_operator(-3.0, "Z 2") result = observable.get_expectation_value(state) output = {'energy': result} return (output)
def _try_append_gate(self, op: ops.GateOperation, qulacs_circuit: qulacs.QuantumCircuit, indices: np.array): # One qubit gate if isinstance(op.gate, ops.pauli_gates._PauliX): qulacs_circuit.add_X_gate(indices[0]) elif isinstance(op.gate, ops.pauli_gates._PauliY): qulacs_circuit.add_Y_gate(indices[0]) elif isinstance(op.gate, ops.pauli_gates._PauliZ): qulacs_circuit.add_Z_gate(indices[0]) elif isinstance(op.gate, ops.common_gates.HPowGate): qulacs_circuit.add_H_gate(indices[0]) elif isinstance(op.gate, ops.common_gates.XPowGate): qulacs_circuit.add_RX_gate(indices[0], -np.pi * op.gate._exponent) elif isinstance(op.gate, ops.common_gates.YPowGate): qulacs_circuit.add_RY_gate(indices[0], -np.pi * op.gate._exponent) elif isinstance(op.gate, ops.common_gates.ZPowGate): qulacs_circuit.add_RZ_gate(indices[0], -np.pi * op.gate._exponent) elif isinstance(op.gate, ops.SingleQubitMatrixGate): mat = op.gate._matrix qulacs_circuit.add_dense_matrix_gate(indices[0], mat) elif isinstance(op.gate, circuits.qasm_output.QasmUGate): lmda = op.gate.lmda theta = op.gate.theta phi = op.gate.phi gate = qulacs.gate.U3(indices[0], theta * np.pi, phi * np.pi, lmda * np.pi) qulacs_circuit.add_gate(gate) # Two qubit gate elif isinstance(op.gate, ops.common_gates.CNotPowGate): if op.gate._exponent == 1.0: qulacs_circuit.add_CNOT_gate(indices[0], indices[1]) else: mat = _get_google_rotx(op.gate._exponent) gate = qulacs.gate.DenseMatrix(indices[1], mat) gate.add_control_qubit(indices[0], 1) qulacs_circuit.add_gate(gate) elif isinstance(op.gate, ops.common_gates.CZPowGate): if op.gate._exponent == 1.0: qulacs_circuit.add_CZ_gate(indices[0], indices[1]) else: mat = _get_google_rotz(op.gate._exponent) gate = qulacs.gate.DenseMatrix(indices[1], mat) gate.add_control_qubit(indices[0], 1) qulacs_circuit.add_gate(gate) elif isinstance(op.gate, ops.common_gates.SwapPowGate): if op.gate._exponent == 1.0: qulacs_circuit.add_SWAP_gate(indices[0], indices[1]) else: qulacs_circuit.add_dense_matrix_gate(indices, op._unitary_()) elif isinstance(op.gate, ops.parity_gates.XXPowGate): qulacs_circuit.add_multi_Pauli_rotation_gate( indices, [1, 1], -np.pi * op.gate._exponent) elif isinstance(op.gate, ops.parity_gates.YYPowGate): qulacs_circuit.add_multi_Pauli_rotation_gate( indices, [2, 2], -np.pi * op.gate._exponent) elif isinstance(op.gate, ops.parity_gates.ZZPowGate): qulacs_circuit.add_multi_Pauli_rotation_gate( indices, [3, 3], -np.pi * op.gate._exponent) elif isinstance(op.gate, ops.TwoQubitMatrixGate): indices.reverse() mat = op.gate._matrix qulacs_circuit.add_dense_matrix_gate(indices, mat) # Three qubit gate """ # deprecated because these functions cause errors in gpu elif isinstance(op.gate, ops.three_qubit_gates.CCXPowGate): mat = _get_google_rotx(op.gate._exponent) gate = qulacs.gate.DenseMatrix(indices[2], mat) gate.add_control_qubit(indices[0],1) gate.add_control_qubit(indices[1],1) qulacs_circuit.add_gate(gate) elif isinstance(op.gate, ops.three_qubit_gates.CCZPowGate): mat = _get_google_rotz(op.gate._exponent) gate = qulacs.gate.DenseMatrix(indices[2], mat) gate.add_control_qubit(indices[0],1) gate.add_control_qubit(indices[1],1) qulacs_circuit.add_gate(gate) """ elif isinstance(op.gate, ops.three_qubit_gates.CSwapGate): mat = np.zeros(shape=(4, 4)) mat[0, 0] = 1 mat[1, 2] = 1 mat[2, 1] = 1 mat[3, 3] = 1 gate = qulacs.gate.DenseMatrix(indices[1:], mat) gate.add_control_qubit(indices[0], 1) qulacs_circuit.add_gate(gate) # Misc elif protocols.has_unitary(op): indices.reverse() mat = op._unitary_() qulacs_circuit.add_dense_matrix_gate(indices, mat) # Not unitary else: return False return True
U = QuantumCircuit(nqubit) angle_y = np.arcsin(x) angle_z = np.arccos(x**2) for i in range(nqubit): U.add_RY_gate(i, angle_y) U.add_RZ_gate(i, angle_z) return U circuit = QuantumCircuit(nqubit) #circuit.add_H_gate(0) #circuit.add_H_gate(1) #circuit.add_H_gate(2) circuit.add_RX_gate(0, 0.1) circuit.add_RX_gate(1, 0.1) #circuit.add_RX_gate(2, 0.1) merged_gate = merge(CNOT(0,1),Y(1)) circuit.add_gate(merged_gate) meas0 = Measurement(0, 0) circuit.add_gate(meas0) meas1 = Measurement(1, 1) circuit.add_gate(meas1) #meas2 = Measurement(2, 2) #circuit.add_gate(meas2) print(circuit) observable = Observable(nqubit) observable.add_operator(1, "Z 0") observable.add_operator(2, "Z 1")
def create_input_gate(self, x, uin_type): # Encode x into quantum state # uin_type: unitary data-input type 0, 1, 20, 21, 30, 31, 40, 41, 50, 51, 60, 61 # x = 1dim. variables, [-1,1] I_mat = np.eye(2, dtype=complex) X_mat = X(0).get_matrix() Y_mat = Y(0).get_matrix() Z_mat = Z(0).get_matrix() #make operators s.t. exp(i*theta * sigma^z_j@sigma^z_k) @:tensor product def ZZ(u, theta, j, k): u.add_CNOT_gate(j, k) u.add_RZ_gate(k, -2 * theta * self.time_step) u.add_CNOT_gate(j, k) return u def XX(u, theta, j, k): u.add_H_gate(j) u.add_H_gate(k) ZZ(u, theta, j, k) u.add_H_gate(j) u.add_H_gate(k) return u def YY(u, theta, j, k): u.add_U1_gate(j, -np.pi / 2.) u.add_U1_gate(k, -np.pi / 2.) XX(u, theta, j, k) u.add_U1_gate(j, np.pi / 2.) u.add_U1_gate(k, np.pi / 2.) return u theta = x u = QuantumCircuit(self.nqubit) angle_y = np.arcsin(x) angle_z = np.arccos(x**2) if uin_type == 0: for i in range(self.nqubit): u.add_RY_gate(i, angle_y[i]) u.add_RZ_gate(i, angle_z[i]) elif uin_type == 1: #for d in range(2): for i in range(self.nqubit): u.add_H_gate(i) u.add_RY_gate(i, angle_y[i]) u.add_RZ_gate(i, angle_z[i]) # KT: add second order expansion for i in range(self.nqubit - 1): for j in range(i + 1, self.nqubit): angle_z2 = np.arccos(x[i] * x[j]) u.add_CNOT_gate(i, j) u.add_RZ_gate(j, angle_z2) u.add_CNOT_gate(i, j) elif uin_type == 20: for i in range(self.nqubit): u.add_RX_gate(i, -2 * x[i] * self.time_step) elif uin_type == 21: ham = np.zeros((2**self.nqubit, 2**self.nqubit), dtype=complex) for i in range(self.nqubit): # i runs 0 to nqubit-1 J_x = x[i] print(x) ham += J_x * make_fullgate([[i, X_mat]], self.nqubit) ## Build time-evolution operator by diagonalizing the Ising hamiltonian H*P = P*D <-> H = P*D*P^dagger diag, eigen_vecs = np.linalg.eigh(ham) time_evol_op = np.dot( np.dot(eigen_vecs, np.diag(np.exp(-1j * self.time_step * diag))), eigen_vecs.T.conj()) # e^-iHT # Convert to qulacs gate time_evol_gate = DenseMatrix([i for i in range(self.nqubit)], time_evol_op) u.add_gate(time_evol_gate) elif uin_type == 30: #Ising hamiltonian with input coefficient # nearest neighbor spin-conbination has interaction for i in range(self.nqubit): u.add_RX_gate(i, -2 * x[i] * self.time_step) ZZ(u, theta[i] * theta[(i + 1) % self.nqubit], i, i + 1) elif uin_type == 31: ham = np.zeros((2**self.nqubit, 2**self.nqubit), dtype=complex) for i in range(self.nqubit): J_x = x[i] ham += J_x * make_fullgate([[i, X_mat]], self.nqubit) J_zz = x[i] * x[(i + 1) % self.nqubit] ham += J_zz * make_fullgate( [[i, Z_mat], [(i + 1) % self.nqubit, Z_mat]], self.nqubit) diag, eigen_vecs = np.linalg.eigh(ham) time_evol_op = np.dot( np.dot(eigen_vecs, np.diag(np.exp(-1j * self.time_step * diag))), eigen_vecs.T.conj()) time_evol_gate = DenseMatrix([i for i in range(self.nqubit)], time_evol_op) u.add_gate(time_evol_gate) elif uin_type == 40: #Ising hamiltonian with input coefficient # every two possible spin-conbination has interaction for i in range(self.nqubit): u.add_RX_gate(i, -2 * x[i] * self.time_step) for j in range(i + 1, self.nqubit): ZZ(u, theta[i] * theta[j], i, j) elif uin_type == 41: ham = np.zeros((2**self.nqubit, 2**self.nqubit), dtype=complex) for i in range(self.nqubit): J_x = x[i] ham += J_x * make_fullgate([[i, X_mat]], self.nqubit) for j in range(i + 1, self.nqubit): J_ij = x[i] * x[j] ham += J_ij * make_fullgate([[i, Z_mat], [j, Z_mat]], self.nqubit) diag, eigen_vecs = np.linalg.eigh(ham) time_evol_op = np.dot( np.dot(eigen_vecs, np.diag(np.exp(-1j * self.time_step * diag))), eigen_vecs.T.conj()) time_evol_gate = DenseMatrix([i for i in range(self.nqubit)], time_evol_op) u.add_gate(time_evol_gate) elif uin_type == 50: #Heisenberg hamiltonian with input coefficient # nearest neighbor spin-conbination has interaction for i in range(self.nqubit): u.add_RX_gate(i, -2 * x[i] * self.time_step) XX(u, theta[i] * theta[(i + 1) % self.nqubit], i, i + 1) YY(u, theta[i] * theta[(i + 1) % self.nqubit], i, i + 1) ZZ(u, theta[i] * theta[(i + 1) % self.nqubit], i, i + 1) elif uin_type == 51: ham = np.zeros((2**self.nqubit, 2**self.nqubit), dtype=complex) for i in range(self.nqubit): J_x = x[i] ham += J_x * make_fullgate([[i, X_mat]], self.nqubit) J_xx = x[i] * x[(i + 1) % self.nqubit] J_yy = x[i] * x[(i + 1) % self.nqubit] J_zz = x[i] * x[(i + 1) % self.nqubit] ham += J_xx * make_fullgate( [[i, X_mat], [(i + 1) % self.nqubit, X_mat]], self.nqubit) ham += J_yy * make_fullgate( [[i, Y_mat], [(i + 1) % self.nqubit, Y_mat]], self.nqubit) ham += J_xx * make_fullgate( [[i, Z_mat], [(i + 1) % self.nqubit, Z_mat]], self.nqubit) diag, eigen_vecs = np.linalg.eigh(ham) time_evol_op = np.dot( np.dot(eigen_vecs, np.diag(np.exp(-1j * self.time_step * diag))), eigen_vecs.T.conj()) time_evol_gate = DenseMatrix([i for i in range(self.nqubit)], time_evol_op) u.add_gate(time_evol_gate) elif uin_type == 60: #Heisenberg hamiltonian with input coefficient # every two possible spin-conbination has interaction for i in range(self.nqubit): u.add_RX_gate(i, -2 * x[i] * self.time_step) for j in range(i + 1, self.nqubit): XX(u, theta[i] * theta[j], i, j) YY(u, theta[i] * theta[j], i, j) ZZ(u, theta[i] * theta[j], i, j) elif uin_type == 61: ham = np.zeros((2**self.nqubit, 2**self.nqubit), dtype=complex) for i in range(self.nqubit): J_x = x[i] ham += J_x * make_fullgate([[i, X_mat]], self.nqubit) for j in range(i + 1, self.nqubit): J_xx = x[i] * x[j] J_yy = x[i] * x[j] J_zz = x[i] * x[j] ham += J_xx * make_fullgate([[i, X_mat], [j, X_mat]], self.nqubit) ham += J_yy * make_fullgate([[i, Y_mat], [j, Y_mat]], self.nqubit) ham += J_xx * make_fullgate([[i, Z_mat], [j, Z_mat]], self.nqubit) diag, eigen_vecs = np.linalg.eigh(ham) time_evol_op = np.dot( np.dot(eigen_vecs, np.diag(np.exp(-1j * self.time_step * diag))), eigen_vecs.T.conj()) time_evol_gate = DenseMatrix([i for i in range(self.nqubit)], time_evol_op) u.add_gate(time_evol_gate) else: pass return u