def test_local_mbasis_qubit_povms(self): """Test qubit povms kwarg""" size = 2 outcomes = 2 qubits = [0, 2] qubit_povms = { qubits[0]: [ [qi.random_density_matrix(2, seed=30 + i + j) for i in range(outcomes)] for j in range(size) ], qubits[1]: [ [qi.random_density_matrix(2, seed=40 + i + j) for i in range(outcomes)] for j in range(size) ], } basis = LocalMeasurementBasis("fitter_basis", qubit_povms=qubit_povms) # Check states indices = it.product(range(size), repeat=len(qubits)) outcomes = it.product(range(outcomes), repeat=len(qubits)) for index in indices: for outcome in outcomes: basis_state = qi.DensityMatrix( basis.matrix(index, self._outcome_tup_to_int(outcome), qubits) ) target0 = qi.DensityMatrix(qubit_povms[qubits[0]][index[0]][outcome[0]]) target1 = qi.DensityMatrix(qubit_povms[qubits[1]][index[1]][outcome[1]]) target = target0.expand(target1) fid = qi.state_fidelity(basis_state, target) self.assertTrue(isclose(fid, 1))
def test_set_density_matrix(self, num_qubits): """Test SetDensityMatrix instruction""" SUPPORTED_METHODS = [ 'automatic', 'density_matrix', 'density_matrix_gpu', 'density_matrix_thrust' ] seed = 100 save_label = 'state' target = qi.random_density_matrix(2 ** num_qubits, seed=seed) circ = QuantumCircuit(num_qubits) circ.set_density_matrix(target) circ.save_density_matrix(label=save_label) # Run opts = self.BACKEND_OPTS.copy() qobj = assemble(circ, self.SIMULATOR) result = self.SIMULATOR.run(qobj, **opts).result() method = opts.get('method', 'automatic') if method not in SUPPORTED_METHODS: self.assertFalse(result.success) else: self.assertTrue(result.success) data = result.data(0) self.assertIn(save_label, data) value = qi.DensityMatrix(result.data(0)[save_label]) self.assertAlmostEqual(value, target)
def test_local_pbasis_default_densitymatrix(self): """Test default states kwarg""" default_states = [qi.random_density_matrix(2, seed=30 + i) for i in range(3)] basis = LocalPreparationBasis("fitter_basis", default_states=default_states) for i, state in enumerate(default_states): basis_state = qi.DensityMatrix(basis.matrix([i], [0])) fid = qi.state_fidelity(state, basis_state) self.assertTrue(isclose(fid, 1))
def test_density_matrix_evolve(self): orig = qi.random_density_matrix(2, seed=10) compat = cqi.DensityMatrix(orig.data) orig_op = qi.random_unitary(2, seed=10) compat_op = cqi.Operator(orig_op.data) target = orig.evolve(orig_op) self.assertEqual(orig.evolve(compat_op), target) self.assertEqual(compat.evolve(orig_op), target) self.assertEqual(compat.evolve(compat_op), target)
def test_local_pbasis_qubit_states_no_default(self): """Test matrix method raises for invalid qubit with no default states""" size = 2 qubits = [0, 2] qubit_states = { qubits[0]: [qi.random_density_matrix(2, seed=30 + i) for i in range(size)], qubits[1]: [qi.random_statevector(2, seed=40 + i) for i in range(size)], } basis = LocalPreparationBasis("fitter_basis", qubit_states=qubit_states) # No default states so should raise an exception with self.assertRaises(ValueError): basis.matrix([0, 0], [0, 1])
def test_local_mbasis_default_and_qubit_states(self): """Test qubit and default povm kwarg""" size = 3 outcomes = 2 qubits = [2, 0] default_povms = ( [ [qi.random_statevector(2, seed=20 + i + j) for i in range(outcomes)] for j in range(size) ], ) qubit_povms = { qubits[0]: [ [qi.random_density_matrix(2, seed=30 + i + j) for i in range(outcomes)] for j in range(size) ], qubits[1]: [ [qi.random_density_matrix(2, seed=40 + i + j) for i in range(outcomes)] for j in range(size) ], } basis = LocalMeasurementBasis( "fitter_basis", default_povms=default_povms, qubit_povms=qubit_povms ) # Check states states0 = qubit_povms[qubits[0]] if qubits[0] in qubit_povms else default_povms states1 = qubit_povms[qubits[1]] if qubits[1] in qubit_povms else default_povms indices = it.product(range(size), repeat=2) outcomes = it.product(range(outcomes), repeat=len(qubits)) for index in indices: for outcome in outcomes: basis_state = qi.DensityMatrix( basis.matrix(index, self._outcome_tup_to_int(outcome), qubits) ) target0 = qi.DensityMatrix(states0[index[0]][outcome[0]]) target1 = qi.DensityMatrix(states1[index[1]][outcome[1]]) target = target0.expand(target1) fid = qi.state_fidelity(basis_state, target) self.assertTrue(isclose(fid, 1))
def test_local_pbasis_default_and_qubit_states(self): """Test qubit states kwarg""" size = 3 qubits = [2, 0] default_states = [qi.random_density_matrix(2, seed=20 + i) for i in range(size)] qubit_states = {2: [qi.random_statevector(2, seed=40 + i) for i in range(size)]} basis = LocalPreparationBasis( "fitter_basis", default_states=default_states, qubit_states=qubit_states ) # Check states indices = it.product(range(size), repeat=2) states0 = qubit_states[qubits[0]] if qubits[0] in qubit_states else default_states states1 = qubit_states[qubits[1]] if qubits[1] in qubit_states else default_states for index in indices: basis_state = qi.DensityMatrix(basis.matrix(index, qubits)) target = qi.DensityMatrix(states0[index[0]]).expand(states1[index[1]]) fid = qi.state_fidelity(basis_state, target) self.assertTrue(isclose(fid, 1))
def test_local_pbasis_qubit_states(self): """Test qubit states kwarg""" size = 3 qubits = [0, 2] qubit_states = { qubits[0]: [qi.random_density_matrix(2, seed=30 + i) for i in range(size)], qubits[1]: [qi.random_statevector(2, seed=40 + i) for i in range(size)], } basis = LocalPreparationBasis("fitter_basis", qubit_states=qubit_states) # Check states indices = it.product(range(size), repeat=2) for index in indices: basis_state = qi.DensityMatrix(basis.matrix(index, qubits)) target0 = qi.DensityMatrix(qubit_states[qubits[0]][index[0]]) target1 = qi.DensityMatrix(qubit_states[qubits[1]][index[1]]) target = target0.expand(target1) fid = qi.state_fidelity(basis_state, target) self.assertTrue(isclose(fid, 1))
def test_set_density_matrix(self, method, device, num_qubits): """Test SetDensityMatrix instruction""" backend = self.backend(method=method, device=device) seed = 100 label = 'state' target = qi.random_density_matrix(2**num_qubits, seed=seed) circ = QuantumCircuit(num_qubits) circ.set_density_matrix(target) circ.save_density_matrix(label=label) # Run result = backend.run(transpile(circ, backend, optimization_level=0), shots=1).result() self.assertTrue(result.success) simdata = result.data(0) self.assertIn(label, simdata) value = simdata[label] self.assertEqual(value, target)
def test_roundtrip_density_matrix(self): """Test round-trip serialization of a DensityMatrix""" obj = qi.random_density_matrix(4, seed=10) self.assertRoundTripSerializable(obj)
def test_density_matrix_eq(self): orig = qi.random_density_matrix(4, seed=10) compat = cqi.DensityMatrix(orig.data) self.assertEqual(compat, orig) self.assertEqual(orig, compat)
def test_density_matrix_tensor(self): orig = qi.random_density_matrix(2, seed=10) compat = cqi.DensityMatrix(orig.data) target = orig.tensor(orig) self.assertEqual(compat.tensor(orig), target) self.assertEqual(orig.tensor(compat), target)
def test_density_matrix_linop(self): orig = qi.random_density_matrix(4, seed=10) compat = cqi.DensityMatrix(orig.data) self.assertEqual(2 * compat - orig, orig) self.assertEqual(2 * orig - compat, orig)