def test_map_numpy_types(self):
     self.assertEqual(
         map_tuples(np.array([1, 2, 3], dtype=np.int32)),
         [1, 2, 3]
     )
     self.assertEqual(
         map_tuples(np.array([1., 2., 3.], dtype=np.single)),
         [1., 2., 3.]
     )
Пример #2
0
    def test_map_ndarray(self):
        self.assertEqual(map_tuples(np.array([1, 2, 3])), [1, 2, 3])

        tuples = [(0, 'Zero'), (1, 'One')]
        tuples_array = np.empty(len(tuples), dtype=object)
        tuples_array[:] = tuples
        self.assertEqual(map_tuples(tuples_array), [{
            '@type': 'tuple',
            'Item1': 0,
            'Item2': 'Zero'
        }, {
            '@type': 'tuple',
            'Item1': 1,
            'Item2': 'One'
        }])
Пример #3
0
 def test_map_deep_tuple(self):
     actual = {
         'foo': [1, 3.14, (42, 'baz')],
         'bar': {
             'a': ('a', 'a'),
             'b': ()
         }
     }
     expected = {
         'foo': [1, 3.14, {
             '@type': 'tuple',
             'item1': 42,
             'item2': 'baz'
         }],
         'bar': {
             'a': {
                 '@type': 'tuple',
                 'item1': 'a',
                 'item2': 'a'
             },
             'b': {
                 '@type': 'tuple'
             }
         }
     }
     self.assertEqual(map_tuples(actual), expected)
Пример #4
0
 def add_terms(self, fermion_terms : Iterable[Tuple[List[int], float]]) -> None:
     """ 
     Adds terms to the fermion Hamiltonian.
     """
     logger.info(f"Adding {len(fermion_terms)} terms to fermion Hamiltonian.")
     args = { 'hamiltonian': self.__dict__, 'fermion_terms': fermion_terms }
     args_json = json.dumps(map_tuples(args))
     result = qsharp.client._execute(f'%chemistry.fh.add_terms {args_json}', raise_on_stderr=True)
     self.__dict__ = result
Пример #5
0
def encode(hamiltonian : FermionHamiltonian, input_state : InputState) -> Tuple:
    """
    Encodes the given Hamiltonian and input state using the Jordan Wigner encoding
    that can be used to run chemistry simulations using Q#'s chemistry library.
    """
    logger.info(f"Doing jw encoding.")
    args = { 'hamiltonian': hamiltonian.__dict__, 'input_state': input_state.__dict__ }
    args_json = json.dumps(map_tuples(args))
    data = qsharp.client._execute(f'%chemistry.encode {args_json}', raise_on_stderr=True)
    return data
Пример #6
0
    def load_fermion_hamiltonian(self, index_convention : IndexConvention = IndexConvention.UpDown) -> FermionHamiltonian:
        """
        Loads the fermion Hamiltonian associated with this electronic structure problem.

        `index_convention` can be 'UpDown' or 'HalfUp'
        """
        logger.info(f"Loading fermion Hamiltonian from problem description using index_convention '{index_convention.name}'.")
        args = { 'problem_description': self.__dict__, 'index_convention': index_convention.name }
        args_json = json.dumps(map_tuples(args))
        data = qsharp.client._execute(f'%chemistry.fh.load {args_json}', raise_on_stderr=True)
        return FermionHamiltonian(data)
Пример #7
0
def load_fermion_hamiltonian(file_name: str, index_convention : IndexConvention = IndexConvention.UpDown) -> FermionHamiltonian:
    """
    Loads the fermion Hamiltonian from the given file that contains broombridge data.

    `index_convention` can be 'UpDown' or 'HalfUp'
    """
    logger.info(f"Loading fermion Hamiltonian from '{file_name}' using index_convention '{index_convention.name}'.")
    args = { 'file_name': file_name, 'index_convention': index_convention.name }
    args_json = json.dumps(map_tuples(args))
    data = qsharp.client._execute(f'%chemistry.fh.load {args_json}', raise_on_stderr=True)
    return FermionHamiltonian(data)
Пример #8
0
 def test_roundtrip_very_deep_tuple(self):
     actual = {
         'a': {
             'b': ({
                 'c': ('d', ['e', ('f', 'g', 12, False)])
             }, ['h', {
                 'g': ('i', 'j')
             }])
         }
     }
     self.assertEqual(unmap_tuples(map_tuples(actual)), actual)
Пример #9
0
def load_input_state(file_name: str, wavefunction_label : str = None, index_convention : IndexConvention = IndexConvention.UpDown) -> FermionHamiltonian:
    """
    Loads the input state associated with the given labe from the given file that contains broombridge data..

    If `wavefunction_label` is not specified, it loads the greedy (Hartree–Fock) state.

    `index_convention` can be 'UpDown' or 'HalfUp'
    """
    logger.info(f"Loading input state '{wavefunction_label}' from '{file_name}' using index_convention '{index_convention.name}'.")
    args = { 'file_name': file_name, 'wavefunction_label': wavefunction_label, 'index_convention': index_convention.name }
    args_json = json.dumps(map_tuples(args))
    data = qsharp.client._execute(f'%chemistry.inputstate.load {args_json}', raise_on_stderr=True)
    return InputState(data)
Пример #10
0
    def load_input_state(self, wavefunction_label ='', index_convention : IndexConvention = IndexConvention.UpDown) -> FermionHamiltonian:
        """
        Loads the input state associated from this electronic structure problem with the corresponding label.

        If `wavefunction_label` is not specified, it loads the greedy (Hartree-Fock) state.

        `index_convention` can be 'UpDown' or 'HalfUp'
        """
        logger.info(f"Loading input state '{wavefunction_label}' from problem description using index_convention '{index_convention.name}'.")
        args = { 'problem_description': self.__dict__, 'wavefunction_label': wavefunction_label, 'index_convention': index_convention.name }
        args_json = json.dumps(map_tuples(args))
        data = qsharp.client._execute(f'%chemistry.inputstate.load {args_json}', raise_on_stderr=True)
        return InputState(data)
Пример #11
0
 def test_roundtrip_deep_tuple(self):
     actual = ('a', ('b', 'c'))
     self.assertEqual(unmap_tuples(map_tuples(actual)), actual)
Пример #12
0
 def test_roundtrip_dict(self):
     actual = {'a': 'b', 'c': ('d', 'e')}
     self.assertEqual(unmap_tuples(map_tuples(actual)), actual)
Пример #13
0
 def test_roundtrip_shallow_tuple(self):
     actual = ('a', 3.14, False)
     self.assertEqual(unmap_tuples(map_tuples(actual)), actual)
Пример #14
0
 def test_map_shallow_tuple(self):
     self.assertEqual(map_tuples((42, 'foo')), {
         '@type': 'tuple',
         'item1': 42,
         'item2': 'foo'
     })