class TestInitialStateZero(QiskitAquaTestCase): def test_qubits_2_vector(self): self.zero = Zero(2) cct = self.zero.construct_circuit('vector') np.testing.assert_array_equal(cct, [1.0, 0.0, 0.0, 0.0]) def test_qubits_5_vector(self): self.zero = Zero(5) cct = self.zero.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_circuit(self): self.zero = Zero(2) cct = self.zero.construct_circuit('circuit') self.assertEqual(cct.qasm(), 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[2];\n') def test_qubits_5_circuit(self): self.zero = Zero(5) cct = self.zero.construct_circuit('circuit') self.assertEqual(cct.qasm(), 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[5];\n')
def test_qubits_5_vector(self): self.zero = Zero(5) cct = self.zero.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_vqe_direct(self, batch_mode): backend = get_aer_backend('statevector_simulator') num_qubits = self.algo_input.qubit_op.num_qubits init_state = Zero(num_qubits) var_form = RY(num_qubits, 3, initial_state=init_state) optimizer = L_BFGS_B() algo = VQE(self.algo_input.qubit_op, var_form, optimizer, 'matrix', batch_mode=batch_mode) quantum_instance = QuantumInstance(backend) result = algo.run(quantum_instance) self.assertAlmostEqual(result['energy'], -1.85727503)
def test_vqe_caching_direct(self, batch_mode): backend = get_aer_backend('statevector_simulator') num_qubits = self.algo_input.qubit_op.num_qubits init_state = Zero(num_qubits) var_form = RY(num_qubits, 3, initial_state=init_state) optimizer = L_BFGS_B() algo = VQE(self.algo_input.qubit_op, var_form, optimizer, 'matrix', batch_mode=batch_mode) circuit_cache = CircuitCache(skip_qobj_deepcopy=True) quantum_instance_caching = QuantumInstance(backend, circuit_cache=circuit_cache, skip_qobj_validation=True) result_caching = algo.run(quantum_instance_caching) self.assertAlmostEqual( self.reference_vqe_result['statevector_simulator']['energy'], result_caching['energy'])
def test_qubits_5_circuit(self): self.zero = Zero(5) cct = self.zero.construct_circuit('circuit') self.assertEqual(cct.qasm(), 'OPENQASM 2.0;\ninclude "qelib1.inc";\nqreg q[5];\n')
"label": "ZZ" }, { "coeff": { "imag": 0.0, "real": 0.18093119978423156 }, "label": "XX" }] } # ====================== # setting up the circuit # ====================== # define the initial state init_state = Zero(num_qubits) # get a variational ansatz ansatz = RY(num_qubits, initial_state=init_state) # operator from hamiltonian qubit_op = Operator.load_from_dict(pauli_dict) # get an optimizer optimizer = COBYLA(maxiter=1000, disp=True) # form the algorithm vqe = VQE(qubit_op, ansatz, optimizer) # get a backend backend = get_aer_backend("statevector_simulator")