def test_snapshot_pauli_type(self): """Test snapshot instruction has correct type.""" pauli_ops = [[[1, 'I'], [0.5, 'X'], [0.25, 'Y'], [-3, 'Z']], [[1j, 'I'], [0.5j, 'X'], [0.25j, 'Y'], [-3j, 'Z']], [[0.5j, Pauli.from_label('X')], [-0.5j, Pauli.from_label('Z')]]] for op in pauli_ops: # standard instrs = [ SnapshotExpectationValue('snap', op, single_shot=False, variance=False).assemble(), self.snapshot_circuit_instr(1, 'snap', op, [0], single_shot=False, variance=False) ] for instr in instrs: self.assertTrue(hasattr(instr, 'snapshot_type')) self.assertEqual(instr.snapshot_type, 'expectation_value_pauli') # Single shot instrs = [ SnapshotExpectationValue('snap', op, single_shot=True, variance=False).assemble(), self.snapshot_circuit_instr(1, 'snap', op, [0], single_shot=True, variance=False) ] for instr in instrs: self.assertTrue(hasattr(instr, 'snapshot_type')) self.assertEqual(instr.snapshot_type, 'expectation_value_pauli_single_shot') # Variance instrs = [ SnapshotExpectationValue('snap', op, single_shot=False, variance=True).assemble(), self.snapshot_circuit_instr(1, 'snap', op, [0], single_shot=False, variance=True) ] for instr in instrs: self.assertTrue(hasattr(instr, 'snapshot_type')) self.assertEqual(instr.snapshot_type, 'expectation_value_pauli_with_variance')
def stabilizes_statevector(stabilizer, statevector): """Return True if two stabilizer states are equal.""" # Get stabilizer and destabilizers and convert to sets for stab in stabilizer: if stab[0] == '-': pauli_mat = -1 * Pauli.from_label(stab[1:]).to_matrix() else: pauli_mat = Pauli.from_label(stab).to_matrix() val = statevector.conj().dot(pauli_mat.dot(statevector)) if not np.isclose(val, 1): return False return True
def test_mul(self): """Test multiplication.""" p1 = self.ref_p p2 = Pauli.from_label('ZXXI') p3 = p1 * p2 self.assertEqual(len(p3), 4) self.assertEqual(p3[:].to_label(), 'ZYIY')
def test_mul(self): """Test multiplication.""" p1 = self.ref_p p2 = Pauli.from_label("ZXXI") p3 = p1.dot(p2) self.assertEqual(len(p3), 4) self.assertEqual(p3[:].to_label(), "ZYIY")
def test_imul(self): """Test in-place multiplication.""" p1 = self.ref_p p2 = Pauli.from_label('ZXXI') p3 = deepcopy(p2) p2 *= p1 self.assertTrue(p2 != p3) self.assertEqual(p2[:].to_label(), 'ZYIY')
def test_snapshot_matrix_type(self): """Test snapshot instruction has correct type.""" matrix_ops = [ numpy.eye(2), numpy.array([[0, 1j], [-1j, 0]]), Operator(Pauli.from_label('Z')) ] for op in matrix_ops: # standard instrs = [ SnapshotExpectationValue('snap', op, single_shot=False, variance=False).assemble(), self.snapshot_circuit_instr(1, 'snap', op, [0], single_shot=False, variance=False) ] for instr in instrs: self.assertTrue(hasattr(instr, 'snapshot_type')) self.assertEqual(instr.snapshot_type, 'expectation_value_matrix') # Single shot instrs = [ SnapshotExpectationValue('snap', op, single_shot=True, variance=False).assemble(), self.snapshot_circuit_instr(1, 'snap', op, [0], single_shot=True, variance=False) ] for instr in instrs: self.assertTrue(hasattr(instr, 'snapshot_type')) self.assertEqual(instr.snapshot_type, 'expectation_value_matrix_single_shot') # Variance instrs = [ SnapshotExpectationValue('snap', op, single_shot=False, variance=True).assemble(), self.snapshot_circuit_instr(1, 'snap', op, [0], single_shot=False, variance=True) ] for instr in instrs: self.assertTrue(hasattr(instr, 'snapshot_type')) self.assertEqual(instr.snapshot_type, 'expectation_value_matrix_with_variance')
def test_snapshot_name(self): """Test snapshot instruction has correct name""" for op in [Pauli.from_label('X'), Operator([[0, 1], [1, 0]])]: instrs = [ SnapshotExpectationValue('snap', op).assemble(), self.snapshot_circuit_instr(1, 'snap', op, [0]) ] for instr in instrs: self.assertTrue(hasattr(instr, 'name')) self.assertEqual(instr.name, 'snapshot')
def test_insert_paulis(self): """Test inserting paulis via pauli object.""" p1 = deepcopy(self.ref_p) new_p = Pauli.from_label('XY') p1.insert_paulis(indices=[0], paulis=new_p) self.assertTrue(p1 != self.ref_p) self.assertEqual(len(p1), 6) self.assertEqual(p1.to_label(), self.ref_label + 'XY')
def test_snapshot_label(self): """Test snapshot instruction has correct label""" for op in [Pauli.from_label('X'), Operator([[0, 1], [1, 0]])]: for label in ['snap0', 'snap1']: instrs = [ SnapshotExpectationValue(label, op).assemble(), self.snapshot_circuit_instr(1, label, op, [0]) ] for instr in instrs: self.assertTrue(hasattr(instr, 'label')) self.assertEqual(instr.label, label)
def test_snapshot_specific_qubits(self): """Test snapshot instruction has correct qubits.""" for qubits in [[0], [0, 2], [1, 3, 0]]: pauli = Pauli.from_label(len(qubits) * 'X') instrs = [ self.snapshot_circuit_instr(5, 'snap', pauli, qubits), self.snapshot_circuit_instr(5, 'snap', Operator(pauli), qubits) ] for instr in instrs: self.assertTrue(hasattr(instr, 'qubits')) self.assertEqual(instr.qubits, qubits)
# Tests the qdrift method by creating a circuit # which time-evolves an H2 molecule # Set up qubits and initial circuit n = 2 qubits = QuantumRegister(2) circuit = QuantumCircuit(qubits) # Perform X on the first qubit to create the initial state of the system circuit.x(qubits[0]) # List of local Hamiltonians # See https://arxiv.org/pdf/1512.06860.pdf#page=9&zoom=100,90,196 H = np.array([ Pauli.from_label('II'), Pauli.from_label('ZI'), Pauli.from_label('IZ'), Pauli.from_label('ZZ'), Pauli.from_label('YY'), Pauli.from_label('XX') ]) # List of corresponding coefficients h = np.array([-0.1927, 0.2048, -0.0929, 0.4588, 0.1116, 0.1116]) t = 1 epsilon = 0.01 circuit = time_evolve_qubits(qubits, circuit, n, H, h, t, epsilon) print(circuit)
adapt_data_df = pd.read_csv('load_adapt_data_df.csv') adapt_data_dict = adapt_data_df.to_dict() Ham_list = adapt_data_dict['hamiltonian'] counter = 0 counter2 = 0 num_hams = 10 pauli_meta_list = [0]*num_hams weight_meta_list = [0]*num_hams Exact_energy_dict = {'Ham_1':[],'Ham_2':[],'Ham_3':[],'Ham_4':[],'Ham_5':[],'Ham_6':[],'Ham_7':[],'Ham_8':[],'Ham_9':[]} for counter in range(0,num_hams,1): Ham = Ham_list[counter] single_ham_list = Ham.split('\n') pauli_list = [0]*(len(single_ham_list)-1) weight_list = [0]*(len(single_ham_list)-1) for counter2 in range(0, len(single_ham_list)-1,1): pauli_list[counter2] = Pauli.from_label(single_ham_list[counter2][:4]) weight_list[counter2] = complex(single_ham_list[counter2][6:-1]) pauli_meta_list[counter] = pauli_list weight_meta_list[counter] = weight_list qubit_op = WeightedPauliOperator.from_list(pauli_list,weight_list) Exact_result = ExactEigensolver(qubit_op, k = 16).run() Exact_energy_dict['Ham_{}'.format(counter)] = Exact_result['energies'] print('E', Exact_result) Exact_energy_df = pd.DataFrame(Exact_energy_dict) Exact_energy_df_file = open("exact_energy_df.csv", "w+") Exact_energy_df.to_csv('exact_energy_df.csv') Exact_energy_df_file.close()