def test_paulisum(self): """Test that the PauliSum Hamiltonians give the ground state energy.""" supported_nspins = [4, 8, 12, 16] for nspins in supported_nspins: qbs = cirq.GridQubit.rect(nspins, 1) circuits, _, pauli_sums, addinfo = spin_system.tfi_chain( qbs, 'closed') qubit_map = {qbs[i]: i for i in range(len(qbs))} for n in range(len(pauli_sums)): phi = cirq.Simulator().simulate(circuits[n]).final_state e = pauli_sums[n].expectation_from_wavefunction(phi, qubit_map) self.assertAllClose(e, addinfo[n].gs_energy, rtol=1e-4)
def test_errors(self): """Test that it errors on invalid arguments.""" with self.assertRaisesRegex(ValueError, expected_regex='Supported number of'): qbs = cirq.GridQubit.rect(3, 1) spin_system.tfi_chain(qbs) with self.assertRaisesRegex( ValueError, expected_regex='Supported boundary conditions'): qbs = cirq.GridQubit.rect(4, 1) spin_system.tfi_chain(qbs, 'open') with self.assertRaisesRegex(TypeError, expected_regex='expected str, bytes '): qbs = cirq.GridQubit.rect(4, 1) spin_system.tfi_chain(qbs, data_dir=123) with self.assertRaisesRegex(TypeError, expected_regex='must be a list of'): spin_system.tfi_chain(['bob']) with self.assertRaisesRegex( TypeError, expected_regex='must be a one-dimensional'): qbs = cirq.GridQubit.rect(4, 1) spin_system.tfi_chain([qbs])
def test_fidelity(self): """Test that all fidelities are close to 1.""" supported_nspins = [4, 8, 12, 16] for nspins in supported_nspins: qbs = cirq.GridQubit.rect(nspins, 1) circuits, _, _, addinfo = spin_system.tfi_chain( qbs, 'closed', ) for n in range(len(addinfo)): phi = cirq.Simulator().simulate(circuits[n]).final_state gs = addinfo[n].gs self.assertAllClose(np.abs(np.conj(np.dot(gs, phi))), 1.0, rtol=1e-3)
def setUpClass(self): """Setup data for the test""" self.random_subset_size = 10 self.supported_nspins_tfi_chain = [4, 8, 12, 16] self.data_dict_tfi_chain = {} self.qbs_dict_tfi_chain = {} for nspins in self.supported_nspins_tfi_chain: qbs_tfi_chain = cirq.GridQubit.rect(nspins, 1) self.data_dict_tfi_chain[nspins] = spin_system.tfi_chain( qbs_tfi_chain, 'closed', ) self.qbs_dict_tfi_chain[nspins] = qbs_tfi_chain self.random_subset_tfi_chain = np.random.permutation(list( range(76)))[:self.random_subset_size] super(TFIChainTest).__init__(self)
def test_returned_objects(self): """Test that the length and types of returned objects are correct.""" supported_nspins = [4, 8, 12, 16] for nspins in supported_nspins: qbs = cirq.GridQubit.rect(nspins, 1) circuits, labels, pauli_sums, addinfo = spin_system.tfi_chain( qbs, 'closed') self.assertLen(circuits, 81) self.assertLen(labels, 81) self.assertLen(pauli_sums, 81) self.assertLen(addinfo, 81) for n in range(81): self.assertIsInstance(circuits[n], cirq.Circuit) self.assertIsInstance(labels[n], int) self.assertIsInstance(pauli_sums[n], cirq.PauliSum) self.assertIsInstance(addinfo[n], SpinSystemInfo)
def test_param_resolver(self): """Test that the length and types of returned objects are correct.""" supported_nspins = [4, 8, 12, 16] for nspins in supported_nspins: qbs = cirq.GridQubit.rect(nspins, 1) circuits, _, _, addinfo = spin_system.tfi_chain(qbs, 'closed') for n in range(81): resolved_circuit = cirq.resolve_parameters( addinfo[n].var_circuit, addinfo[n].params) state_circuit = cirq.Simulator().simulate( circuits[n]).final_state state_resolved_circuit = cirq.Simulator().simulate( resolved_circuit).final_state self.assertAllClose(np.abs( np.conj(np.dot(state_circuit, state_resolved_circuit))), 1.0, rtol=1e-3)