class TestInitialStateCustom(QiskitAquaTestCase): def test_qubits_2_zero_vector(self): self.custom = Custom(2, state='zero') cct = self.custom.construct_circuit('vector') np.testing.assert_array_equal(cct, [1.0, 0.0, 0.0, 0.0]) def test_qubits_5_zero_vector(self): self.custom = Custom(5, state='zero') cct = self.custom.construct_circuit('vector') np.testing.assert_array_equal(cct, [ 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ]) def test_qubits_2_zero_circuit(self): self.custom = Custom(2, state='zero') cct = self.custom.construct_circuit('circuit') self.assertEqual(cct.qasm(), 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[2];\n') def test_qubits_5_zero_circuit(self): self.custom = Custom(5, state='zero') cct = self.custom.construct_circuit('circuit') self.assertEqual(cct.qasm(), 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[5];\n') def test_qubits_2_uniform_vector(self): self.custom = Custom(2, state='uniform') cct = self.custom.construct_circuit('vector') np.testing.assert_array_equal(cct, [0.5] * 4) def test_qubits_5_uniform_vector(self): self.custom = Custom(5, state='uniform') cct = self.custom.construct_circuit('vector') np.testing.assert_array_almost_equal(cct, [0.1767767] * 32) def test_qubits_2_uniform_circuit(self): self.custom = Custom(2, state='uniform') cct = self.custom.construct_circuit('circuit') self.assertEqual( cct.qasm(), 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[2];\n' 'u2(0.0,3.14159265358979) q[0];\nu2(0.0,3.14159265358979) q[1];\n') def test_qubits_2_random_vector(self): self.custom = Custom(2, state='random') cct = self.custom.construct_circuit('vector') prob = np.sqrt(np.sum([x**2 for x in cct])) self.assertAlmostEqual(prob, 1.0) def test_qubits_5_random_vector(self): self.custom = Custom(5, state='random') cct = self.custom.construct_circuit('vector') prob = np.sqrt(np.sum([x**2 for x in cct])) self.assertAlmostEqual(prob, 1.0) def test_qubits_2_given_vector(self): self.custom = Custom(2, state_vector=[0.5] * 4) cct = self.custom.construct_circuit('vector') np.testing.assert_array_equal(cct, [0.5] * 4) def test_qubits_5_given_vector(self): self.custom = Custom(5, state_vector=[1.0] * 32) cct = self.custom.construct_circuit('vector') np.testing.assert_array_almost_equal(cct, [0.1767767] * 32) def test_qubits_5_randgiven_vector(self): self.custom = Custom(5, state_vector=np.random.rand(32)) cct = self.custom.construct_circuit('vector') prob = np.sqrt(np.sum([x**2 for x in cct])) self.assertAlmostEqual(prob, 1.0) def test_qubits_qubits_given_mistmatch(self): with self.assertRaises(ValueError): self.custom = Custom(5, state_vector=[1.0] * 23) def test_qubits_2_zero_vector_wrong_cct_mode(self): self.custom = Custom(5, state='zero') with self.assertRaises(ValueError): cct = self.custom.construct_circuit('matrix')
def test_evolution(self): SIZE = 2 #SPARSITY = 0 #X = [[0, 1], [1, 0]] #Y = [[0, -1j], [1j, 0]] Z = [[1, 0], [0, -1]] I = [[1, 0], [0, 1]] # + 0.5 * np.kron(Y, X)# + 0.3 * np.kron(Z, X) + 0.4 * np.kron(Z, Y) h1 = np.kron(I, Z) # np.random.seed(2) temp = np.random.random((2**SIZE, 2**SIZE)) h1 = temp + temp.T qubit_op = Operator(matrix=h1) # qubit_op_jw.chop_by_threshold(10 ** -10) if qubit_op.grouped_paulis is None: qubit_op._matrix_to_paulis() qubit_op._paulis_to_grouped_paulis() for ps in qubit_op.grouped_paulis: for p1 in ps: for p2 in ps: if p1 != p2: np.testing.assert_almost_equal( p1[1].to_matrix() @ p2[1].to_matrix(), p2[1].to_matrix() @ p1[1].to_matrix()) state_in = Custom(SIZE, state='random') evo_time = 1 num_time_slices = 3 # announces params self.log.debug('evo time: {}'.format(evo_time)) self.log.debug('num time slices: {}'.format(num_time_slices)) self.log.debug('state_in: {}'.format(state_in._state_vector)) # get the exact state_out from raw matrix multiplication state_out_exact = qubit_op.evolve(state_in.construct_circuit('vector'), evo_time, 'matrix', 0) # self.log.debug('exact:\n{}'.format(state_out_exact)) qubit_op_temp = copy.deepcopy(qubit_op) for grouping in ['default', 'random']: self.log.debug('Under {} paulis grouping:'.format(grouping)) for expansion_mode in ['trotter', 'suzuki']: self.log.debug( 'Under {} expansion mode:'.format(expansion_mode)) for expansion_order in [ 1, 2, 3, 4 ] if expansion_mode == 'suzuki' else [1]: # assure every time the operator from the original one qubit_op = copy.deepcopy(qubit_op_temp) if expansion_mode == 'suzuki': self.log.debug( 'With expansion order {}:'.format(expansion_order)) state_out_matrix = qubit_op.evolve( state_in.construct_circuit('vector'), evo_time, 'matrix', num_time_slices, paulis_grouping=grouping, expansion_mode=expansion_mode, expansion_order=expansion_order) quantum_registers = QuantumRegister(qubit_op.num_qubits) qc = state_in.construct_circuit('circuit', quantum_registers) qc += qubit_op.evolve( None, evo_time, 'circuit', num_time_slices, quantum_registers=quantum_registers, paulis_grouping=grouping, expansion_mode=expansion_mode, expansion_order=expansion_order, ) job = q_execute( qc, qiskit.Aer.get_backend('statevector_simulator')) state_out_circuit = np.asarray( job.result().get_statevector(qc, decimals=16)) self.log.debug( 'The fidelity between exact and matrix: {}'.format( state_fidelity(state_out_exact, state_out_matrix))) self.log.debug( 'The fidelity between exact and circuit: {}'.format( state_fidelity(state_out_exact, state_out_circuit))) f_mc = state_fidelity(state_out_matrix, state_out_circuit) self.log.debug( 'The fidelity between matrix and circuit: {}'.format( f_mc)) self.assertAlmostEqual(f_mc, 1)