def test_adding_gate(self) -> None: circuit = Circuit(1) assert circuit.get_parallelism() == 0 circuit.append_gate(U3Gate(), [0]) assert circuit.get_parallelism() == 1 circuit.append_gate(U3Gate(), [0]) assert circuit.get_parallelism() == 1 circuit.append_gate(U3Gate(), [0]) assert circuit.get_parallelism() == 1
def test_adding_gate(self) -> None: circuit = Circuit(1) assert circuit.get_depth() == 0 circuit.append_gate(U3Gate(), [0]) assert circuit.get_depth() == 1 circuit.append_gate(U3Gate(), [0]) assert circuit.get_depth() == 2 circuit.append_gate(U3Gate(), [0]) assert circuit.get_depth() == 3
def test_single_qubit_1(self) -> None: circuit = Circuit(1) assert len(circuit.get_coupling_graph()) == 0 circuit.append_gate(U3Gate(), [0]) assert len(circuit.get_coupling_graph()) == 0 circuit.append_gate(U3Gate(), [0]) assert len(circuit.get_coupling_graph()) == 0 circuit.append_gate(U3Gate(), [0]) assert len(circuit.get_coupling_graph()) == 0
def test_adding_gate_3(self) -> None: circuit = Circuit(2) assert circuit.get_parallelism() == 0 circuit.append_gate(U3Gate(), [0]) assert circuit.get_parallelism() == 1 circuit.append_gate(U3Gate(), [1]) assert circuit.get_parallelism() == 2 circuit.append_gate(CNOTGate(), [0, 1]) assert circuit.get_parallelism() == 2
def test_adding_gate(self) -> None: circuit = Circuit(1) assert circuit.get_num_operations() == 0 circuit.append_gate(U3Gate(), [0]) assert circuit.get_num_operations() == 1 circuit.append_gate(U3Gate(), [0]) assert circuit.get_num_operations() == 2 circuit.append_gate(U3Gate(), [0]) assert circuit.get_num_operations() == 3
def test_inserting_gate(self) -> None: circuit = Circuit(1) assert circuit.get_num_operations() == 0 circuit.insert_gate(0, U3Gate(), [0]) assert circuit.get_num_operations() == 1 circuit.insert_gate(0, U3Gate(), [0]) assert circuit.get_num_operations() == 2 circuit.insert_gate(0, U3Gate(), [0]) assert circuit.get_num_operations() == 3
def test_inserting_gate(self) -> None: circuit = Circuit(1) assert circuit.get_num_params() == 0 circuit.insert_gate(0, U3Gate(), [0]) assert circuit.get_num_params() == 3 circuit.insert_gate(0, U3Gate(), [0]) assert circuit.get_num_params() == 6 circuit.insert_gate(0, U3Gate(), [0]) assert circuit.get_num_params() == 9
def test_adding_gate(self) -> None: circuit = Circuit(1) assert circuit.get_num_params() == 0 circuit.append_gate(U3Gate(), [0]) assert circuit.get_num_params() == 3 circuit.append_gate(U3Gate(), [0]) assert circuit.get_num_params() == 6 circuit.append_gate(U3Gate(), [0]) assert circuit.get_num_params() == 9
def test_inserting_gate(self) -> None: circuit = Circuit(1) assert circuit.get_depth() == 0 circuit.insert_gate(0, U3Gate(), [0]) assert circuit.get_depth() == 1 circuit.insert_gate(0, U3Gate(), [0]) assert circuit.get_depth() == 2 circuit.insert_gate(0, U3Gate(), [0]) assert circuit.get_depth() == 3
def test_r1(self, r3_qubit_circuit: Circuit) -> None: start = r3_qubit_circuit.get_num_params() r3_qubit_circuit.append_gate(U3Gate(), [0]) assert r3_qubit_circuit.get_num_params() == start + 3 r3_qubit_circuit.insert_gate(0, U3Gate(), [1]) assert r3_qubit_circuit.get_num_params() == start + 6 r3_qubit_circuit.insert_gate(0, CNOTGate(), [0, 2]) assert r3_qubit_circuit.get_num_params() == start + 6 r3_qubit_circuit.remove(U3Gate()) assert r3_qubit_circuit.get_num_params() == start + 3
def test_single_qubit_2(self) -> None: circuit = Circuit(4) assert len(circuit.get_coupling_graph()) == 0 for i in range(4): circuit.append_gate(U3Gate(), [i]) assert len(circuit.get_coupling_graph()) == 0 for j in range(4): for i in range(4): circuit.append_gate(U3Gate(), [i]) assert len(circuit.get_coupling_graph()) == 0
def test_r1(self, r3_qubit_circuit: Circuit) -> None: assert r3_qubit_circuit.get_num_operations() == 10 r3_qubit_circuit.append_gate(U3Gate(), [0]) assert r3_qubit_circuit.get_num_operations() == 11 r3_qubit_circuit.insert_gate(0, U3Gate(), [1]) assert r3_qubit_circuit.get_num_operations() == 12 r3_qubit_circuit.insert_gate(0, CNOTGate(), [0, 2]) assert r3_qubit_circuit.get_num_operations() == 13 r3_qubit_circuit.remove(U3Gate()) assert r3_qubit_circuit.get_num_operations() == 12 r3_qubit_circuit.remove(CNOTGate()) assert r3_qubit_circuit.get_num_operations() == 11
def test_freezing_param(self) -> None: circuit = Circuit(1) circuit.append_gate(U3Gate(), [0]) circuit.append_gate(U3Gate(), [0]) circuit.append_gate(U3Gate(), [0]) assert circuit.get_num_params() == 9 circuit.freeze_param(0) assert circuit.get_num_params() == 8 circuit.freeze_param(0) assert circuit.get_num_params() == 7 circuit.freeze_param(0) assert circuit.get_num_params() == 6 circuit.freeze_param(0)
def test_adding_gate(self) -> None: circuit = Circuit(1) 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()
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()
def test_removing_gate(self) -> None: circuit = Circuit(1) circuit.append_gate(U3Gate(), [0]) circuit.append_gate(U3Gate(), [0]) circuit.append_gate(U3Gate(), [0]) assert circuit.get_num_params() == 9 circuit.remove(U3Gate()) assert circuit.get_num_params() == 6 circuit.remove(U3Gate()) assert circuit.get_num_params() == 3 circuit.remove(U3Gate()) assert circuit.get_num_params() == 0
def test_removing_gate(self) -> None: circuit = Circuit(1) circuit.append_gate(U3Gate(), [0]) circuit.append_gate(U3Gate(), [0]) circuit.append_gate(U3Gate(), [0]) assert circuit.get_num_operations() == 3 circuit.remove(U3Gate()) assert circuit.get_num_operations() == 2 circuit.remove(U3Gate()) assert circuit.get_num_operations() == 1 circuit.remove(U3Gate()) assert circuit.get_num_operations() == 0
def test_removing_gate1(self) -> None: circuit = Circuit(1) circuit.append_gate(U3Gate(), [0]) circuit.append_gate(U3Gate(), [0]) circuit.append_gate(U3Gate(), [0]) assert circuit.get_depth() == 3 circuit.remove(U3Gate()) assert circuit.get_depth() == 2 circuit.remove(U3Gate()) assert circuit.get_depth() == 1 circuit.remove(U3Gate()) assert circuit.get_depth() == 0
def test_removing_gate2(self) -> None: circuit = Circuit(2) circuit.append_gate(U3Gate(), [0]) circuit.append_gate(CNOTGate(), [0, 1]) circuit.append_gate(U3Gate(), [0]) circuit.append_gate(U3Gate(), [1]) assert circuit.get_num_cycles() == 3 circuit.remove(U3Gate()) assert circuit.get_num_cycles() == 2 circuit.remove(CNOTGate()) assert circuit.get_num_cycles() == 1 circuit.remove(U3Gate()) assert circuit.get_num_cycles() == 1 circuit.remove(U3Gate()) assert circuit.get_num_cycles() == 0
def test_adding_gate_2(self) -> None: circuit = Circuit(2) assert circuit.get_parallelism() == 0 circuit.append_gate(U3Gate(), [0]) assert circuit.get_parallelism() == 1 circuit.append_gate(U3Gate(), [1]) assert circuit.get_parallelism() == 2 circuit.append_gate(U3Gate(), [0]) assert circuit.get_parallelism() == 1.5 circuit.append_gate(U3Gate(), [1]) assert circuit.get_parallelism() == 2 circuit.append_gate(U3Gate(), [0]) assert circuit.get_parallelism() - 5 / 3 < 1e-12 circuit.append_gate(U3Gate(), [1]) assert circuit.get_parallelism() == 2
from bqskit.ir.gates import U2Gate from bqskit.ir.gates import U3Gate from bqskit.ir.gates import VariableUnitaryGate from bqskit.ir.gates.parameterized.pauli import PauliGate NATIVE_GATES: list[Gate] = [ RXGate(), RYGate(), RZGate(), RXXGate(), RYYGate(), RZZGate(), CNOTGate(), U1Gate(), U2Gate(), U3Gate(), PauliGate(1), PauliGate(2), PauliGate(3), ] NON_GRADIENT_GATES: list[Gate] = [ VariableUnitaryGate(2), VariableUnitaryGate(3), VariableUnitaryGate(4), ] @pytest.mark.parametrize( 'gate', NATIVE_GATES + NON_GRADIENT_GATES,
SXGate(), SqrtXGate(), TGate(), TdgGate(), ConstantUnitaryGate(TOFFOLI), # TODO XGate(), XXGate(), YGate(), ZGate(), # PauliGate(), # TODO RXGate(), RYGate(), RZGate(), U1Gate(), U2Gate(), U3Gate(), U8Gate(), DaggerGate(TGate()), DaggerGate(CZGate()), DaggerGate(U1Gate()), DaggerGate(U8Gate()), FrozenParameterGate(U1Gate(), {0: np.pi}), FrozenParameterGate(U3Gate(), {0: np.pi}), FrozenParameterGate(U3Gate(), { 0: np.pi / 2, 1: np.pi / 2, 2: np.pi / 2 }), FrozenParameterGate(U8Gate(), {0: np.pi}), FrozenParameterGate(U8Gate(), { 0: np.pi / 2,
def __init__( self, two_qudit_gate: Gate = CNOTGate(), single_qudit_gate_1: Gate = U3Gate(), single_qudit_gate_2: Gate | None = None, initial_layer_gate: Gate | None = None, ) -> None: """ Construct a SimpleLayerGenerator. Args: two_qudit_gate (Gate): A two-qudit gate that starts this layer generator's building block. (Default: CNOTGate()) single_qudit_gate_1 (Gate): A single-qudit gate that follows `two_qudit_gate` in the building block. (Default: U3Gate()) single_qudit_gate_2 (Gate | None): An alternate single-qudit gate to be used as the second single-qudit gate in the building block. If left as None, defaults to `single_qudit_gate_1`. (Default: None) initial_layer_gate (Gate | None): An alternate single-qudit gate that creates the initial layer. If left as None, defaults to `single_qudit_gate_1`. (Default: None) Raises: ValueError: If `two_qudit_gate`'s size is not 2, or if any of the single-qudit gates' size is not 1. ValueError: If `single_qudit_gate_1`'s radix does not match the radix of `two_qudit_gate`'s first qudit, or if `single_qudit_gate_2`'s radix does not match the radix of `two_qudit_gate`'s second qudit. """ if not isinstance(two_qudit_gate, Gate): raise TypeError( 'Expected gate for two_qudit_gate, got %s.' % type(two_qudit_gate), ) if two_qudit_gate.get_size() != 2: raise ValueError( 'Expected two-qudit gate' ', got a gate that acts on %d qudits.' % two_qudit_gate.get_size(), ) if not isinstance(single_qudit_gate_1, Gate): raise TypeError( 'Expected gate for single_qudit_gate_1, got %s.' % type(single_qudit_gate_1), ) if single_qudit_gate_1.get_size() != 1: raise ValueError( 'Expected single-qudit gate' ', got a gate that acts on %d qudits.' % single_qudit_gate_1.get_size(), ) if single_qudit_gate_2 is None: single_qudit_gate_2 = single_qudit_gate_1 if initial_layer_gate is None: initial_layer_gate = single_qudit_gate_1 if not isinstance(single_qudit_gate_2, Gate): raise TypeError( 'Expected gate for single_qudit_gate_2, got %s.' % type(single_qudit_gate_2), ) if single_qudit_gate_2.get_size() != 1: raise ValueError( 'Expected single-qudit gate' ', got a gate that acts on %d qudits.' % single_qudit_gate_2.get_size(), ) if not isinstance(initial_layer_gate, Gate): raise TypeError( 'Expected gate for initial_layer_gate, got %s.' % type(initial_layer_gate), ) if initial_layer_gate.get_size() != 1: raise ValueError( 'Expected single-qudit gate' ', got a gate that acts on %d qudits.' % initial_layer_gate.get_size(), ) two_radix_1 = two_qudit_gate.get_radixes()[0] two_radix_2 = two_qudit_gate.get_radixes()[1] if two_radix_1 != single_qudit_gate_1.get_radixes()[0]: raise ValueError( 'Radix mismatch between two_qudit_gate and single_qudit_gate_1' ': %d != %d.' % (two_radix_1, single_qudit_gate_1.get_radixes()[0]), ) if two_radix_2 != single_qudit_gate_2.get_radixes()[0]: raise ValueError( 'Radix mismatch between two_qudit_gate and single_qudit_gate_2' ': %d != %d.' % (two_radix_2, single_qudit_gate_2.get_radixes()[0]), ) self.two_qudit_gate = two_qudit_gate self.single_qudit_gate_1 = single_qudit_gate_1 self.single_qudit_gate_2 = single_qudit_gate_2 self.initial_layer_gate = initial_layer_gate