def get_exp(H, circ): psi = CircuitStateFn(circ) backend = Aer.get_backend('statevector_simulator') measurable_expression = StateFn(H, is_measurement=True).compose(psi) expectation = AerPauliExpectation().convert(measurable_expression) sampler = CircuitSampler(backend).convert(expectation) return sampler.eval().real
def test_coefficients_correctly_propagated(self): """Test that the coefficients in SummedOp and states are correctly used.""" with self.subTest('zero coeff in SummedOp'): op = 0 * (I + Z) state = Plus self.assertEqual((~StateFn(op) @ state).eval(), 0j) backend = Aer.get_backend('qasm_simulator') op = I with self.subTest('zero coeff in summed StateFn and CircuitSampler'): state = 0 * (Plus + Minus) sampler = CircuitSampler(backend).convert(~StateFn(op) @ state) self.assertEqual(sampler.eval(), 0j) with self.subTest( 'coeff gets squared in CircuitSampler shot-based readout'): state = (Plus + Minus) / numpy.sqrt(2) sampler = CircuitSampler(backend).convert(~StateFn(op) @ state) self.assertAlmostEqual(sampler.eval(), 1 + 0j)
def test_circuit_sampler2(self, method): """Test the probability gradient with the circuit sampler dp0/da = cos(a)sin(b) / 2 dp1/da = - cos(a)sin(b) / 2 dp0/db = sin(a)cos(b) / 2 dp1/db = - sin(a)cos(b) / 2 """ a = Parameter('a') b = Parameter('b') params = [a, b] q = QuantumRegister(1) qc = QuantumCircuit(q) qc.h(q) qc.rz(params[0], q[0]) qc.rx(params[1], q[0]) op = CircuitStateFn(primitive=qc, coeff=1.) shots = 8000 if method == 'fin_diff': np.random.seed(8) prob_grad = Gradient(grad_method=method, epsilon=shots**(-1 / 6.)).convert( operator=op, params=params) else: prob_grad = Gradient(grad_method=method).convert(operator=op, params=params) values_dict = [{ a: [np.pi / 4], b: [0] }, { params[0]: [np.pi / 4], params[1]: [np.pi / 4] }, { params[0]: [np.pi / 2], params[1]: [np.pi] }] correct_values = [[[0, 0], [1 / (2 * np.sqrt(2)), -1 / (2 * np.sqrt(2))]], [[1 / 4, -1 / 4], [1 / 4, -1 / 4]], [[0, 0], [-1 / 2, 1 / 2]]] backend = BasicAer.get_backend('qasm_simulator') q_instance = QuantumInstance(backend=backend, shots=shots) for i, value_dict in enumerate(values_dict): sampler = CircuitSampler(backend=q_instance).convert( prob_grad, params=value_dict) result = sampler.eval() np.testing.assert_array_almost_equal(result[0], correct_values[i], decimal=1)
def test_circuit_sampler(self, method): """Test the gradient with circuit sampler Tr(|psi><psi|Z) = sin(a)sin(b) Tr(|psi><psi|X) = cos(a) d<H>/da = - 0.5 sin(a) - 1 cos(a)sin(b) d<H>/db = - 1 sin(a)cos(b) """ ham = 0.5 * X - 1 * Z a = Parameter('a') b = Parameter('b') params = [a, b] q = QuantumRegister(1) qc = QuantumCircuit(q) qc.h(q) qc.rz(params[0], q[0]) qc.rx(params[1], q[0]) op = ~StateFn(ham) @ CircuitStateFn(primitive=qc, coeff=1.) shots = 8000 if method == 'fin_diff': np.random.seed(8) state_grad = Gradient(grad_method=method, epsilon=shots**(-1 / 6.)).convert( operator=op, params=params) else: state_grad = Gradient(grad_method=method).convert(operator=op, params=params) values_dict = [{ a: np.pi / 4, b: np.pi }, { params[0]: np.pi / 4, params[1]: np.pi / 4 }, { params[0]: np.pi / 2, params[1]: np.pi / 4 }] correct_values = [[-0.5 / np.sqrt(2), 1 / np.sqrt(2)], [-0.5 / np.sqrt(2) - 0.5, -1 / 2.], [-0.5, -1 / np.sqrt(2)]] backend = BasicAer.get_backend('qasm_simulator') q_instance = QuantumInstance(backend=backend, shots=shots) for i, value_dict in enumerate(values_dict): sampler = CircuitSampler(backend=q_instance).convert( state_grad, params={k: [v] for k, v in value_dict.items()}) np.testing.assert_array_almost_equal(sampler.eval()[0], correct_values[i], decimal=1)
def test_ibmq_grouped_pauli_expectation(self): """ pauli expect op vector state vector test """ from qiskit import IBMQ p = IBMQ.load_account() backend = p.get_backend('ibmq_qasm_simulator') paulis_op = ListOp([X, Y, Z, I]) states_op = ListOp([One, Zero, Plus, Minus]) valids = [[+0, 0, 1, -1], [+0, 0, 0, 0], [-1, 1, 0, -0], [+1, 1, 1, 1]] converted_meas = self.expect.convert(~StateFn(paulis_op) @ states_op) sampled = CircuitSampler(backend).convert(converted_meas) np.testing.assert_array_almost_equal(sampled.eval(), valids, decimal=1)
# define your backend or quantum instance (where you can add settings) backend = Aer.get_backend('qasm_simulator') q_instance = QuantumInstance(backend, shots=1024) # define the state to sample measurable_expression = StateFn(op, is_measurement=True).compose(psi) # convert to expectation value expectation = PauliExpectation().convert(measurable_expression) # get state sampler (you can also pass the backend directly) sampler = CircuitSampler(q_instance).convert(expectation) # evaluate print('Sampled:', sampler.eval().real) # %% print(qc) print('Hamiltonian of Ising model:') print(H.print_details()) # %% Randomly sample Clifford circuits and calculate <H> for each one. # (E.g. only change single qubit gates, not their locations in the circuit) # import random # import quimb.tensor as qtn # import quimb as qu # %config InlineBackend.figure_formats = ['svg'] #