def test_pauli_error_1q_unitary_from_pauli(self): """Test single-qubit pauli error as unitary qobj from Pauli obj""" paulis = [Pauli.from_label(s) for s in ['I', 'X', 'Y', 'Z']] probs = [0.4, 0.3, 0.2, 0.1] error = pauli_error(zip(paulis, probs), standard_gates=False) target_unitaries = [ standard_gate_unitary('x'), standard_gate_unitary('y'), standard_gate_unitary('z') ] target_probs = probs.copy() target_identity_count = 0 for j in range(len(paulis)): circ, p = error.error_term(j) name = circ[0]['name'] self.assertIn(name, ('unitary', 'id')) self.assertEqual(circ[0]['qubits'], [0]) self.remove_if_found(p, target_probs) if name == "unitary": self.remove_if_found(circ[0]['params'][0], target_unitaries) else: target_identity_count += 1 self.assertEqual(target_probs, [], msg="Incorrect probabilities") self.assertEqual(target_unitaries, [], msg="Incorrect unitaries") self.assertEqual(target_identity_count, 1, msg="Incorrect identities")
def test_pauli_error_2q_gate_from_pauli(self): """Test two-qubit pauli error as gate qobj from Pauli obj""" paulis = [Pauli.from_label(s) for s in ['XZ', 'YX', 'ZY']] probs = [0.5, 0.3, 0.2] error = pauli_error(zip(paulis, probs), standard_gates=True) target_circs = [[{ "name": "z", "qubits": [0] }, { "name": "x", "qubits": [1] }], [{ "name": "x", "qubits": [0] }, { "name": "y", "qubits": [1] }], [{ "name": "y", "qubits": [0] }, { "name": "z", "qubits": [1] }]] target_probs = probs.copy() for j in range(len(paulis)): circ, p = error.error_term(j) self.remove_if_found(p, target_probs) self.remove_if_found(circ, target_circs) self.assertEqual(target_probs, [], msg="Incorrect probabilities") self.assertEqual(target_circs, [], msg="Incorrect circuits")
def test_pauli_error_2q_unitary_from_pauli(self): """Test two-qubit pauli error as unitary qobj from Pauli obj""" paulis = [Pauli.from_label(s) for s in ['XY', 'YZ', 'ZX']] probs = [0.5, 0.3, 0.2] error = pauli_error(zip(paulis, probs), standard_gates=False) X = standard_gate_unitary('x') Y = standard_gate_unitary('y') Z = standard_gate_unitary('z') target_unitaries = [np.kron(X, Y), np.kron(Y, Z), np.kron(Z, X)] target_probs = probs.copy() for j in range(len(paulis)): circ, p = error.error_term(j) name = circ[0]['name'] self.assertIn(name, 'unitary') self.assertEqual(circ[0]['qubits'], [0, 1]) self.remove_if_found(p, target_probs) self.remove_if_found(circ[0]['params'], target_unitaries) self.assertEqual(target_probs, [], msg="Incorrect probabilities") self.assertEqual(target_unitaries, [], msg="Incorrect unitaries")
def run_exp(num_reps=1): # choice of Hi basis H_basis = [Pauli.from_label(p) for p in ['II', 'ZI', 'IZ', 'ZZ', 'YY', 'XX']] num_qubits = 2 evo_time = 1 epsilon = 0.1 L = 32 ## number of local hamiltonian terms ############################################################ # Generate a random Hamiltonian H as the sum of m basis Hi operators ############################################################ hs = np.random.random(L) indexes = np.random.randint(low=0, high=6, size=L) ## H in matrix form H_matrix = np.zeros((2 ** num_qubits, 2 ** num_qubits)) ## H as a list of pauli operators (unweighted) H_list = [] for i in range(L): H_matrix = H_matrix + hs[i] * H_basis[indexes[i]].to_matrix() H_list.append(H_basis[indexes[i]]) print('matrix H: \n', H_matrix) # H as a pauli operator H_qubitOp = op_converter.to_weighted_pauli_operator(MatrixOperator(matrix=H_matrix)) # Generate an initial state state_in = Custom(num_qubits, state='random') ############################################################ # Ground truth and benchmarks ############################################################ # Ground truth state_in_vec = state_in.construct_circuit('vector') groundtruth = expm(-1.j * H_matrix * evo_time) @ state_in_vec print('The directly computed groundtruth evolution result state is') print('{}\n.'.format(groundtruth)) # Build circuit using Qiskit's evolve algorithm, which based on Trotter-Suzuki. quantum_registers = QuantumRegister(num_qubits) circuit = state_in.construct_circuit('circuit', quantum_registers) circuit += H_qubitOp.evolve( None, evo_time, num_time_slices=10, quantum_registers=quantum_registers, expansion_mode='suzuki', expansion_order=1 ) # Simulate Trotter-Suzuki circuit and print it backend = BasicAer.get_backend('statevector_simulator') job = q_execute(circuit, backend) circuit_execution_result = np.asarray(job.result().get_statevector(circuit)) print('The simulated (suzuki) evolution result state is') print('{}\n'.format(circuit_execution_result)) # The difference between the ground truth and the simulated state # measured by "Fidelity" fidelity_suzuki = state_fidelity(groundtruth, circuit_execution_result) print('Fidelity between the groundtruth and the circuit result states is {}.'.format(fidelity_suzuki)) print('\n') ############################################################ # Our qdrift implementation ############################################################ quantum_registers = QuantumRegister(num_qubits) circuit = state_in.construct_circuit('circuit', quantum_registers) # Contruct the circuit which implements qdrift circuit = time_evolve_qubits(quantum_registers, circuit, num_qubits, H_list, hs, evo_time, epsilon, num_reps) # Simulate circuit and print it backend = BasicAer.get_backend('statevector_simulator') job = q_execute(circuit, backend) circuit_execution_result = np.asarray(job.result().get_statevector(circuit)) print('The simulated (qdrift) evolution result state is\n{}.'.format(circuit_execution_result)) print('\n') # Measure the fidelity fidelity_qdrift = state_fidelity(groundtruth, circuit_execution_result) print('Fidelity between the groundtruth and the circuit result states is {}.'.format(fidelity_qdrift)) print('\n') print('benchmark, suzuki:', fidelity_suzuki) print('qdrift:', fidelity_qdrift) return fidelity_qdrift, fidelity_suzuki