• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

feihoo87 / waveforms / 6534953321

16 Oct 2023 02:19PM UTC coverage: 35.674% (-22.7%) from 58.421%
6534953321

push

github

feihoo87
fix Coveralls

5913 of 16575 relevant lines covered (35.67%)

3.21 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

28.95
/waveforms/qlisp/qasm/node/external.py
1
# This code is part of Qiskit.
2
#
3
# (C) Copyright IBM 2017.
4
#
5
# This code is licensed under the Apache License, Version 2.0. You may
6
# obtain a copy of this license in the LICENSE.txt file in the root directory
7
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
8
#
9
# Any modifications or derivative works of this code must retain this
10
# copyright notice, and modified files need to carry a notice indicating
11
# that they have been altered from the originals.
12

13
"""Node for an OPENQASM external function."""
9✔
14

15
import warnings
9✔
16
import numpy as np
9✔
17

18
from .node import Node
9✔
19
from .nodeexception import NodeException
9✔
20

21

22
class External(Node):
9✔
23
    """Node for an OPENQASM external function.
24
    children[0] is an id node with the name of the function.
25
    children[1] is an expression node.
26
    """
27

28
    def __init__(self, children):
9✔
29
        """Create the external node."""
30
        super().__init__('external', children, None)
×
31

32
    def qasm(self, prec=None):
9✔
33
        """Return the corresponding OPENQASM string."""
34
        if prec is not None:
×
35
            warnings.warn('Parameter \'External.qasm(..., prec)\' is no longer used and is being '
×
36
                          'deprecated.', DeprecationWarning, 2)
37
        return self.children[0].qasm() + "(" + self.children[1].qasm() + ")"
×
38

39
    def latex(self, prec=None, nested_scope=None):
9✔
40
        """Return the corresponding math mode latex string."""
41
        if prec is not None:
×
42
            warnings.warn('Parameter \'External.latex(..., prec)\' is no longer used and is being '
×
43
                          'deprecated.', DeprecationWarning, 2)
44
        if nested_scope is not None:
×
45
            warnings.warn('Parameter \'External.latex(..., nested_scope)\' is no longer used and '
×
46
                          'is being deprecated.', DeprecationWarning, 2)
47
        try:
×
48
            from pylatexenc.latexencode import utf8tolatex
×
49
        except ImportError:
×
50
            raise ImportError("To export latex from qasm "
×
51
                              "pylatexenc needs to be installed. Run "
52
                              "'pip install pylatexenc' before using this "
53
                              "method.")
54
        return utf8tolatex(self.sym())
×
55

56
    def real(self, nested_scope=None):
9✔
57
        """Return the correspond floating point number."""
58
        op = self.children[0].name
×
59
        expr = self.children[1]
×
60
        dispatch = {
×
61
            'sin': np.sin,
62
            'cos': np.cos,
63
            'tan': np.tan,
64
            'asin': np.arcsin,
65
            'acos': np.arccos,
66
            'atan': np.arctan,
67
            'exp': np.exp,
68
            'ln': np.log,
69
            'sqrt': np.sqrt
70
        }
71
        if op in dispatch:
×
72
            arg = expr.real(nested_scope)
×
73
            return dispatch[op](arg)
×
74
        else:
75
            raise NodeException("internal error: undefined external")
×
76

77
    def sym(self, nested_scope=None):
9✔
78
        """Return the corresponding symbolic expression."""
79
        op = self.children[0].name
×
80
        expr = self.children[1]
×
81
        dispatch = {
×
82
            'sin': np.sin,
83
            'cos': np.cos,
84
            'tan': np.tan,
85
            'asin': np.arcsin,
86
            'acos': np.arccos,
87
            'atan': np.arctan,
88
            'exp': np.exp,
89
            'ln': np.log,
90
            'sqrt': np.sqrt
91
        }
92
        if op in dispatch:
×
93
            arg = expr.sym(nested_scope)
×
94
            return dispatch[op](arg)
×
95
        else:
96
            raise NodeException("internal error: undefined external")
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc