Esempio n. 1
0
 def __str__(self):
     """Renders a ASCII diagram showing the board position."""
     s = ''
     s += ' +-------------------------+\n'
     for y in reversed(range(self.size)):
         s += str(y + 1) + '| '
         for x in range(self.size):
             piece = c.PIECES[self._pieces[to_square(x, y)]]
             bit = bu.xy_to_bit(x, y)
             p = self._probs[bit]
             if p < 0.01:
                 piece = '.'
             s += piece + '  '
         s += '|\n |'
         for x in range(self.size):
             bit = bu.xy_to_bit(x, y)
             p = self._probs[bit]
             if 0.01 < p < 0.99:
                 prob = str(int(100 * p))
                 if len(prob) <= 2:
                     s += ' '
                 s += prob
                 if len(prob) < 2:
                     s += ' '
             else:
                 s += '   '
         s += ' |\n'
     s += ' +-------------------------+\n   '
     for x in range(self.size):
         s += to_rank(x) + '  '
     return s
Esempio n. 2
0
    def path_qubits(self, source: str, target: str) -> List[cirq.Qid]:
        """Returns all entangled qubits (or classical pieces)
        between source and target.

        Source and target should be specified in algebraic notation,
        such as 'f4'.
        """
        rtn = []
        xs = move.x_of(source)
        ys = move.y_of(source)
        xt = move.x_of(target)
        yt = move.y_of(target)
        if xt > xs:
            dx = 1
        elif xt < xs:
            dx = -1
        else:
            dx = 0
        if yt > ys:
            dy = 1
        elif yt < ys:
            dy = -1
        else:
            dy = 0
        max_slide = max(abs(xs - xt), abs(ys - yt))
        if max_slide > 1:
            for t in range(1, max_slide):
                path_bit = xy_to_bit(xs + dx * t, ys + dy * t)
                path_qubit = bit_to_qubit(path_bit)
                if (path_qubit in self.entangled_squares
                        or nth_bit_of(path_bit, self.state)):
                    rtn.append(path_qubit)
        return rtn
Esempio n. 3
0
 def _bit_board(self) -> int:
     rtn = 0
     for x in range(self.size):
         for y in range(self.size):
             s = to_square(x, y)
             if self._pieces[s] != c.EMPTY:
                 rtn = bu.set_nth_bit(bu.xy_to_bit(x, y), rtn, True)
     return rtn
Esempio n. 4
0
    def path_qubits(self, source: str, target: str) -> List[cirq.Qid]:
        """Returns all entangled qubits (or classical pieces)
        between source and target.

        Source and target should be in the same line, i.e. same row, 
        same column, or same diagonal.

        Source and target should be specified in algebraic notation,
        such as 'f4'.
        """
        rtn = []
        xs = move.x_of(source)
        ys = move.y_of(source)
        xt = move.x_of(target)
        yt = move.y_of(target)
        if xt > xs:
            dx = 1
        elif xt < xs:
            dx = -1
        else:
            dx = 0
        if yt > ys:
            dy = 1
        elif yt < ys:
            dy = -1
        else:
            dy = 0
        x_slide = abs(xt - xs)
        y_slide = abs(yt - ys)
        # Souce and target should always be in the same line.
        if x_slide != y_slide and x_slide * y_slide:
            raise ValueError(
                'Wrong inputs for path_qubits: source and target are not in the same line.'
            )
        max_slide = max(x_slide, y_slide)
        # Only calculates path when max_slide > 1.
        for t in range(1, max_slide):
            path_bit = xy_to_bit(xs + dx * t, ys + dy * t)
            path_qubit = bit_to_qubit(path_bit)
            if (path_qubit in self.entangled_squares
                    or nth_bit_of(path_bit, self.state)):
                rtn.append(path_qubit)
        return rtn
Esempio n. 5
0
 def __str__(self):
     """Renders a ASCII diagram showing the board probabilities."""
     probs = self.get_probability_distribution()
     s = ''
     s += ' +----------------------------------+\n'
     for y in reversed(range(8)):
         s += str(y + 1) + '| '
         for x in range(8):
             bit = xy_to_bit(x, y)
             prob = str(int(100 * probs[bit]))
             if len(prob) <= 2:
                 s += ' '
             if prob == '0':
                 s += '.'
             else:
                 s += prob
             if len(prob) < 2:
                 s += ' '
             s += ' '
         s += ' |\n'
     s += ' +----------------------------------+\n    '
     for x in range(8):
         s += move.to_rank(x) + '   '
     return s
Esempio n. 6
0
 def __str__(self):
     """Renders a ASCII diagram showing the board probabilities."""
     probs = self.get_probability_distribution()
     s = ""
     s += " +----------------------------------+\n"
     for y in reversed(range(8)):
         s += str(y + 1) + "| "
         for x in range(8):
             bit = xy_to_bit(x, y)
             prob = str(int(100 * probs[bit]))
             if len(prob) <= 2:
                 s += " "
             if prob == "0":
                 s += "."
             else:
                 s += prob
             if len(prob) < 2:
                 s += " "
             s += " "
         s += " |\n"
     s += " +----------------------------------+\n    "
     for x in range(8):
         s += move.to_rank(x) + "   "
     return s