Example #1
0
 def test_snapshot_matrix_type(self):
     """Test snapshot instruction has correct type."""
     matrix_ops = [
         numpy.eye(2),
         numpy.array([[0, 1j], [-1j, 0]]),
         Operator(Pauli('Z'))
     ]
     for op in matrix_ops:
         # standard
         instrs = [
             SnapshotExpectationValue('snap',
                                      op,
                                      single_shot=False,
                                      variance=False).assemble(),
             self.snapshot_circuit_instr(1,
                                         'snap',
                                         op, [0],
                                         single_shot=False,
                                         variance=False)
         ]
         for instr in instrs:
             self.assertTrue(hasattr(instr, 'snapshot_type'))
             self.assertEqual(instr.snapshot_type,
                              'expectation_value_matrix')
         # Single shot
         instrs = [
             SnapshotExpectationValue('snap',
                                      op,
                                      single_shot=True,
                                      variance=False).assemble(),
             self.snapshot_circuit_instr(1,
                                         'snap',
                                         op, [0],
                                         single_shot=True,
                                         variance=False)
         ]
         for instr in instrs:
             self.assertTrue(hasattr(instr, 'snapshot_type'))
             self.assertEqual(instr.snapshot_type,
                              'expectation_value_matrix_single_shot')
         # Variance
         with self.assertWarns(DeprecationWarning):
             instrs = [
                 SnapshotExpectationValue('snap',
                                          op,
                                          single_shot=False,
                                          variance=True).assemble(),
                 self.snapshot_circuit_instr(1,
                                             'snap',
                                             op, [0],
                                             single_shot=False,
                                             variance=True)
             ]
         for instr in instrs:
             self.assertTrue(hasattr(instr, 'snapshot_type'))
             self.assertEqual(instr.snapshot_type,
                              'expectation_value_matrix_with_variance')
Example #2
0
 def test_snapshot_pauli_type(self):
     """Test snapshot instruction has correct type."""
     pauli_ops = [[[1, 'I'], [0.5, 'X'], [0.25, 'Y'], [-3, 'Z']],
                  [[1j, 'I'], [0.5j, 'X'], [0.25j, 'Y'], [-3j, 'Z']],
                  [[0.5j, Pauli('X')], [-0.5j, Pauli('Z')]]]
     for op in pauli_ops:
         # standard
         instrs = [
             SnapshotExpectationValue('snap',
                                      op,
                                      single_shot=False,
                                      variance=False).assemble(),
             self.snapshot_circuit_instr(1,
                                         'snap',
                                         op, [0],
                                         single_shot=False,
                                         variance=False)
         ]
         for instr in instrs:
             self.assertTrue(hasattr(instr, 'snapshot_type'))
             self.assertEqual(instr.snapshot_type,
                              'expectation_value_pauli')
         # Single shot
         instrs = [
             SnapshotExpectationValue('snap',
                                      op,
                                      single_shot=True,
                                      variance=False).assemble(),
             self.snapshot_circuit_instr(1,
                                         'snap',
                                         op, [0],
                                         single_shot=True,
                                         variance=False)
         ]
         for instr in instrs:
             self.assertTrue(hasattr(instr, 'snapshot_type'))
             self.assertEqual(instr.snapshot_type,
                              'expectation_value_pauli_single_shot')
         # Variance
         with self.assertWarns(DeprecationWarning):
             instrs = [
                 SnapshotExpectationValue('snap',
                                          op,
                                          single_shot=False,
                                          variance=True).assemble(),
                 self.snapshot_circuit_instr(1,
                                             'snap',
                                             op, [0],
                                             single_shot=False,
                                             variance=True)
             ]
         for instr in instrs:
             self.assertTrue(hasattr(instr, 'snapshot_type'))
             self.assertEqual(instr.snapshot_type,
                              'expectation_value_pauli_with_variance')
Example #3
0
 def test_snapshot_name(self):
     """Test snapshot instruction has correct name"""
     for op in [Pauli('X'), Operator([[0, 1], [1, 0]])]:
         instrs = [
             SnapshotExpectationValue('snap', op).assemble(),
             self.snapshot_circuit_instr(1, 'snap', op, [0])
         ]
         for instr in instrs:
             self.assertTrue(hasattr(instr, 'name'))
             self.assertEqual(instr.name, 'snapshot')
Example #4
0
 def test_snapshot_label(self):
     """Test snapshot instruction has correct label"""
     for op in [Pauli('X'), Operator([[0, 1], [1, 0]])]:
         for label in ['snap0', 'snap1']:
             instrs = [
                 SnapshotExpectationValue(label, op).assemble(),
                 self.snapshot_circuit_instr(1, label, op, [0])
             ]
             for instr in instrs:
                 self.assertTrue(hasattr(instr, 'label'))
                 self.assertEqual(instr.label, label)
Example #5
0
 def test_snapshot_label_raises(self):
     """Test snapshot label must be str"""
     self.assertRaises(ExtensionError,
                       lambda: SnapshotExpectationValue(1, [[1, 'X']]))