Beispiel #1
0
    def test_correctness_3(self) -> None:
        circuit = Circuit(5)
        wide_gate = IdentityGate(3)
        circuit.append_gate(HGate(), [1])
        circuit.append_gate(CNOTGate(), [2, 3])
        circuit.append_gate(wide_gate, [1, 2, 3])
        circuit.append_gate(CNOTGate(), [1, 2])
        circuit.append_gate(HGate(), [3])
        circuit.append_gate(XGate(), [0])
        circuit.append_gate(XGate(), [0])
        circuit.append_gate(XGate(), [0])
        circuit.append_gate(XGate(), [4])
        circuit.append_gate(XGate(), [4])
        circuit.append_gate(XGate(), [4])
        utry = circuit.get_unitary()

        circuit.fold(circuit.get_region([(0, 2), (1, 1), (2, 1)]))
        assert circuit.get_num_operations() == 9
        assert circuit.get_depth() == 3
        assert circuit.count(HGate()) == 2
        assert circuit.count(XGate()) == 6
        assert isinstance(circuit[1, 1].gate, CircuitGate)
        test_gate: CircuitGate = circuit[1, 1].gate
        assert test_gate._circuit[0, 1].gate is CNOTGate()
        assert test_gate._circuit[0, 2].gate is CNOTGate()
        assert test_gate._circuit[1, 0].gate is wide_gate
        assert test_gate._circuit[1, 1].gate is wide_gate
        assert test_gate._circuit[1, 2].gate is wide_gate
        check_no_idle_cycles(circuit)
        assert np.allclose(utry.get_numpy(), circuit.get_unitary().get_numpy())
Beispiel #2
0
 def get_value(
     self,
     circuit: Circuit,
     target: UnitaryMatrix | StateVector,
 ) -> float:
     """Return the heuristic's value, see HeuristicFunction for more info."""
     cost = 0.0
     for gate in circuit.get_gate_set():
         if gate.get_size() == 1:
             continue
         cost += float(circuit.count(gate))
     return cost
Beispiel #3
0
    def run(self, circuit: Circuit, data: dict[str, Any]) -> None:
        """Perform the pass's operation, see BasePass for more info."""
        stats: dict[str, Any] = {}
        stats['cycles'] = circuit.get_num_cycles()
        stats['num_ops'] = circuit.get_num_operations()
        stats['cgraph'] = circuit.get_coupling_graph()
        stats['depth'] = circuit.get_depth()
        stats['gate_counts'] = {
            gate: circuit.count(gate)
            for gate in circuit.get_gate_set()
        }

        if self.key not in data:
            data[self.key] = []

        data[self.key].append(stats)
Beispiel #4
0
    def get_truth_value(self, circuit: Circuit, data: dict[str, Any]) -> bool:
        """Call this predicate, see PassPredicate for more info."""
        gate_count = circuit.count(self.gate)

        # If first call, record data and return true
        if self.key not in data:
            _logger.debug(f'Could not find {self.key} key.')
            data[self.key] = gate_count
            return True

        # Otherwise, check for a change and return accordingly
        if data[self.key] == gate_count:
            _logger.debug('Counts match; no change detected.')
            return False

        data[self.key] = gate_count
        _logger.debug('Counts do not match; change detected.')
        return True