Exemplo n.º 1
0
    def get_violation_report(circuit: Circuit) -> str:
        """
        Return a message explaining why `circuit` cannot be instantiated.

        Args:
            circuit (Circuit): Generate a report for this circuit.

        Raises:
            ValueError: If `circuit` can be instantiated with this
                instantiater.
        """

        invalid_gates = {
            gate
            for gate in circuit.get_gate_set()
            if not isinstance(gate, LocallyOptimizableUnitary)
        }

        if len(invalid_gates) == 0:
            raise ValueError('Circuit can be instantiated.')

        return (
            'Cannot instantiate circuit with qfactor'
            ' because the following gates are not locally optimizable: %s.'
            % ', '.join(str(g) for g in invalid_gates)
        )
Exemplo n.º 2
0
 def test_value(self, r6_qudit_circuit: Circuit) -> None:
     assert (r6_qudit_circuit.is_parameterized() !=
             r6_qudit_circuit.is_constant())
     if all(g.is_constant() for g in r6_qudit_circuit.get_gate_set()):
         assert r6_qudit_circuit.is_constant()
     else:
         assert not r6_qudit_circuit.is_constant()
Exemplo n.º 3
0
 def test_get_gate_set(self, toffoli_circuit: Circuit) -> None:
     gate_set = toffoli_circuit.get_gate_set()
     assert isinstance(gate_set, set)
     assert len(gate_set) == 4
     assert CNOTGate() in gate_set
     assert HGate() in gate_set
     assert TdgGate() in gate_set
     assert TGate() in gate_set
Exemplo n.º 4
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
Exemplo n.º 5
0
    def encode(self, circuit: Circuit) -> str:
        """Write `circuit` in this language."""
        if not circuit.is_qubit_only():
            raise LangException('Only qubit circuits can be wrriten to qasm.')

        source = "OPENQASM 2.0;\ninclude \"qelib1.inc\";\n"
        source += f'qreg q[{circuit.get_size()}];\n'
        for gate in circuit.get_gate_set():
            source += gate.get_qasm_gate_def()

        for op in circuit:
            source += op.get_qasm()

        return source
Exemplo n.º 6
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)
Exemplo n.º 7
0
 def test_qudit(self) -> None:
     circuit = Circuit(3, [2, 3, 3])
     assert len(circuit.get_gate_set()) == 0
     circuit.append_gate(U3Gate(), [0])
     assert len(circuit.get_gate_set()) == 1
     assert U3Gate() in circuit.get_gate_set()
     circuit.append_gate(XGate(), [0])
     assert len(circuit.get_gate_set()) == 2
     assert U3Gate() in circuit.get_gate_set()
     assert XGate() in circuit.get_gate_set()
     circuit.append_gate(ZGate(), [0])
     assert len(circuit.get_gate_set()) == 3
     assert U3Gate() in circuit.get_gate_set()
     assert XGate() in circuit.get_gate_set()
     assert ZGate() in circuit.get_gate_set()
     circuit.append_gate(TGate(), [0])
     assert len(circuit.get_gate_set()) == 4
     assert U3Gate() in circuit.get_gate_set()
     assert XGate() in circuit.get_gate_set()
     assert ZGate() in circuit.get_gate_set()
     assert TGate() in circuit.get_gate_set()
     circuit.append_gate(CSUMGate(), [1, 2])
     assert len(circuit.get_gate_set()) == 5
     assert U3Gate() in circuit.get_gate_set()
     assert XGate() in circuit.get_gate_set()
     assert ZGate() in circuit.get_gate_set()
     assert TGate() in circuit.get_gate_set()
     assert CSUMGate() in circuit.get_gate_set()
Exemplo n.º 8
0
 def test_removing_gate(self) -> None:
     circuit = Circuit(1)
     assert len(circuit.get_gate_set()) == 0
     circuit.append_gate(U3Gate(), [0])
     circuit.append_gate(XGate(), [0])
     circuit.append_gate(ZGate(), [0])
     circuit.append_gate(TGate(), [0])
     assert len(circuit.get_gate_set()) == 4
     assert U3Gate() in circuit.get_gate_set()
     assert XGate() in circuit.get_gate_set()
     assert ZGate() in circuit.get_gate_set()
     assert TGate() in circuit.get_gate_set()
     circuit.remove(TGate())
     assert len(circuit.get_gate_set()) == 3
     assert U3Gate() in circuit.get_gate_set()
     assert XGate() in circuit.get_gate_set()
     assert ZGate() in circuit.get_gate_set()
     circuit.remove(XGate())
     assert len(circuit.get_gate_set()) == 2
     assert U3Gate() in circuit.get_gate_set()
     assert ZGate() in circuit.get_gate_set()
     circuit.remove(ZGate())
     assert len(circuit.get_gate_set()) == 1
     assert U3Gate() in circuit.get_gate_set()
     circuit.remove(U3Gate())
     assert len(circuit.get_gate_set()) == 0
Exemplo n.º 9
0
 def test_empty(self) -> None:
     circuit = Circuit(4)
     assert len(circuit.get_gate_set()) == 0
     assert isinstance(circuit.get_gate_set(), set)
Exemplo n.º 10
0
 def test_type(self, r6_qudit_circuit: Circuit) -> None:
     assert isinstance(r6_qudit_circuit.get_gate_set(), set)
     assert all(
         isinstance(gate, Gate) for gate in r6_qudit_circuit.get_gate_set())
Exemplo n.º 11
0
 def test_get_gate_set(self, swap_circuit: Circuit) -> None:
     gate_set = swap_circuit.get_gate_set()
     assert isinstance(gate_set, set)
     assert len(gate_set) == 1
     assert CNOTGate() in gate_set
Exemplo n.º 12
0
 def test_get_gate_set(self, simple_circuit: Circuit) -> None:
     gate_set = simple_circuit.get_gate_set()
     assert isinstance(gate_set, set)
     assert len(gate_set) == 2
     assert XGate() in gate_set
     assert CNOTGate() in gate_set
Exemplo n.º 13
0
 def is_capable(circuit: Circuit) -> bool:
     """Return true if the circuit can be instantiated."""
     return all(
         isinstance(gate, LocallyOptimizableUnitary)
         for gate in circuit.get_gate_set()
     )