def test_add_circuit_with_target_and_non_continuous_qubits(): widget = Circuit().h(5).h(50).h(100) circ = Circuit().add_circuit(widget, target=[1, 3, 5]) expected = (Circuit().add_instruction(Instruction( Gate.H(), 1)).add_instruction(Instruction( Gate.H(), 3)).add_instruction(Instruction(Gate.H(), 5))) assert circ == expected
def test_equality(): instr_1 = Instruction(Gate.H(), 0) instr_2 = Instruction(Gate.H(), 0) other_instr = Instruction(Gate.CNot(), [0, 1]) non_instr = "non instruction" assert instr_1 == instr_2 assert instr_1 is not instr_2 assert instr_1 != other_instr assert instr_1 != non_instr
def test_basis_rotation_instructions_all(): circ = Circuit().h(0).cnot(0, 1).sample(observable=Observable.Y()) expected = [ Instruction(Gate.Z(), 0), Instruction(Gate.S(), 0), Instruction(Gate.H(), 0), Instruction(Gate.Z(), 1), Instruction(Gate.S(), 1), Instruction(Gate.H(), 1), ] assert circ.basis_rotation_instructions == expected
def test_add_verbatim_box_different_qubits(): circ = Circuit().h(1).add_verbatim_box(Circuit().h(0)).cnot(3, 4) expected = (Circuit().add_instruction(Instruction( Gate.H(), 1)).add_instruction(Instruction( compiler_directives.StartVerbatimBox())).add_instruction( Instruction(Gate.H(), 0)).add_instruction( Instruction( compiler_directives.EndVerbatimBox())).add_instruction( Instruction(Gate.CNot(), [3, 4]))) assert circ == expected
def test_basis_rotation_instructions_tensor_product(): circ = (Circuit().h(0).cnot(0, 1).expectation( observable=Observable.X() @ Observable.Y() @ Observable.Y(), target=[0, 1, 2])) expected = [ Instruction(Gate.H(), 0), Instruction(Gate.Z(), 1), Instruction(Gate.S(), 1), Instruction(Gate.H(), 1), Instruction(Gate.Z(), 2), Instruction(Gate.S(), 2), Instruction(Gate.H(), 2), ] assert circ.basis_rotation_instructions == expected
def many_layers(n_qubits: int, n_layers: int) -> Circuit: """ Function to return circuit with many layers. :param int n_qubits: number of qubits :param int n_layers: number of layers :return: Constructed easy circuit :rtype: Circuit """ qubits = range(n_qubits) circuit = Circuit() # instantiate circuit object for q in range(n_qubits): circuit.h(q) for layer in range(n_layers): if (layer + 1) % 100 != 0: for qubit in range(len(qubits)): angle = np.random.uniform(0, 2 * math.pi) gate = np.random.choice( [Gate.Rx(angle), Gate.Ry(angle), Gate.Rz(angle), Gate.H()], 1, replace=True )[0] circuit.add_instruction(Instruction(gate, qubit)) else: for q in range(0, n_qubits, 2): circuit.cnot(q, q + 1) for q in range(1, n_qubits - 1, 2): circuit.cnot(q, q + 1) return circuit
def test_subroutine_returns_instruction(): @circuit.subroutine() def foo(target): return Instruction(Gate.H(), 0) circ = Circuit().add(foo, 0) assert circ == Circuit(Instruction(Gate.H(), 0))
def test_add_circuit_with_target(bell_pair): circ = Circuit().add_circuit(bell_pair, target=[10, 11]) expected = (Circuit().add_instruction(Instruction( Gate.H(), 10)).add_instruction(Instruction(Gate.CNot(), [10, 11])).add_result_type( ResultType.Probability([10, 11]))) assert circ == expected
def test_subroutine_returns_iterable(): @circuit.subroutine() def foo(target): for qubit in range(1): yield Instruction(Gate.H(), qubit) circ = Circuit().add(foo, 0) assert circ == Circuit(Instruction(Gate.H(), 0))
def test_basis_rotation_instructions_multiple_result_types_same_targets(): circ = (Circuit().h(0).cnot(0, 1).expectation( observable=Observable.H() @ Observable.X(), target=[0, 1]).sample(observable=Observable.H() @ Observable.X(), target=[0, 1]).variance( observable=Observable.H() @ Observable.X(), target=[0, 1])) expected = [Instruction(Gate.Ry(-np.pi / 4), 0), Instruction(Gate.H(), 1)] assert circ.basis_rotation_instructions == expected
def test_add_verbatim_box_no_preceding(): circ = Circuit().add_verbatim_box(Circuit().h(0)).cnot(2, 3) expected = (Circuit().add_instruction( Instruction(compiler_directives.StartVerbatimBox())).add_instruction( Instruction(Gate.H(), 0)).add_instruction( Instruction( compiler_directives.EndVerbatimBox())).add_instruction( Instruction(Gate.CNot(), [2, 3]))) assert circ == expected
def test_subroutine_register(): # register a private method to avoid Sphinx docs picking this up @circuit.subroutine(register=True) def _foo(target): """this docstring will be added to the registered attribute""" return Instruction(Gate.H(), target) circ = Circuit()._foo(0) assert circ == Circuit(Instruction(Gate.H(), 0)) assert Circuit._foo.__doc__ == _foo.__doc__
def test_basis_rotation_instructions_identity(): circ = (Circuit().h(0).cnot(0, 1).cnot(1, 2).cnot(2, 3).cnot( 3, 4).expectation(observable=Observable.X(), target=[ 0 ]).expectation(observable=Observable.I(), target=[2]).expectation( observable=Observable.I() @ Observable.Y(), target=[1, 3]).expectation(observable=Observable.I(), target=[ 0 ]).expectation(observable=Observable.X() @ Observable.I(), target=[1, 3]).expectation(observable=Observable.Y(), target=[2])) expected = [ Instruction(Gate.H(), 0), Instruction(Gate.H(), 1), Instruction(Gate.Z(), 2), Instruction(Gate.S(), 2), Instruction(Gate.H(), 2), Instruction(Gate.Z(), 3), Instruction(Gate.S(), 3), Instruction(Gate.H(), 3), ] assert circ.basis_rotation_instructions == expected
def test_subroutine_nested(): @circuit.subroutine() def h(target): for qubit in target: yield Instruction(Gate.H(), qubit) @circuit.subroutine() def h_nested(target): for qubit in target: yield h(target) circ = Circuit().add(h_nested, [0, 1]) expected = Circuit( [Instruction(Gate.H(), j) for i in range(2) for j in range(2)]) assert circ == expected
def _foo(target): """this docstring will be added to the registered attribute""" return Instruction(Gate.H(), target)
def h_instr(): return Instruction(Gate.H(), 0)
def test_operator_setter(instr): instr.operator = Gate.H()
def test_basis_rotation_instructions_target(): circ = Circuit().h(0).cnot(0, 1).expectation(observable=Observable.X(), target=0) expected = [Instruction(Gate.H(), 0)] assert circ.basis_rotation_instructions == expected
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. import math import numpy as np import pytest from braket.circuits import Gate, Observable from braket.circuits.observables import observable_from_ir from braket.circuits.quantum_operator_helpers import get_pauli_eigenvalues testdata = [ (Observable.I(), Gate.I(), ["i"], (), np.array([1, 1])), (Observable.X(), Gate.X(), ["x"], tuple([Gate.H()]), get_pauli_eigenvalues(1)), ( Observable.Y(), Gate.Y(), ["y"], tuple([Gate.Z(), Gate.S(), Gate.H()]), get_pauli_eigenvalues(1), ), (Observable.Z(), Gate.Z(), ["z"], (), get_pauli_eigenvalues(1)), (Observable.H(), Gate.H(), ["h"], tuple([Gate.Ry(-math.pi / 4)]), get_pauli_eigenvalues(1)), ] invalid_hermitian_matrices = [ (np.array([[1]])), (np.array([1])), (np.array([0, 1, 2])),
def h(): return Circuit().add_instruction(Instruction(Gate.H(), 0))
def h(target): for qubit in target: yield Instruction(Gate.H(), qubit)
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF # ANY KIND, either express or implied. See the License for the specific # language governing permissions and limitations under the License. import math import numpy as np import pytest from braket.circuits import Gate, Observable from braket.circuits.observables import observable_from_ir from braket.circuits.quantum_operator_helpers import get_pauli_eigenvalues testdata = [ (Observable.I(), Gate.I(), ["i"], (), np.array([1, 1])), (Observable.X(), Gate.X(), ["x"], tuple([Gate.H()]), get_pauli_eigenvalues(1)), ( Observable.Y(), Gate.Y(), ["y"], tuple([Gate.Z(), Gate.S(), Gate.H()]), get_pauli_eigenvalues(1), ), (Observable.Z(), Gate.Z(), ["z"], (), get_pauli_eigenvalues(1)), (Observable.H(), Gate.H(), ["h"], tuple([Gate.Ry(-math.pi / 4)]), get_pauli_eigenvalues(1)), ] invalid_hermitian_matrices = [ (np.array([[1]])),
def foo(target): for qubit in range(1): yield Instruction(Gate.H(), qubit)
def foo(target): return Instruction(Gate.H(), 0)
def h(q): return Instruction(Gate.H(), q)
def test_matrix_equivalence(): gate1 = Gate.H() gate2 = Gate.H() gate3 = Gate.CNot() assert gate1.matrix_equivalence(gate2) assert not gate2.matrix_equivalence(gate3)
def test_matrix_equivalence_non_gate(): gate1 = Gate.H() assert not gate1.matrix_equivalence(1)
( Gate.Y(), [4], OpenQASMSerializationProperties( qubit_reference_type=QubitReferenceType.VIRTUAL), "y q[4];", ), ( Gate.Y(), [4], OpenQASMSerializationProperties( qubit_reference_type=QubitReferenceType.PHYSICAL), "y $4;", ), ( Gate.H(), [4], OpenQASMSerializationProperties( qubit_reference_type=QubitReferenceType.VIRTUAL), "h q[4];", ), ( Gate.H(), [4], OpenQASMSerializationProperties( qubit_reference_type=QubitReferenceType.PHYSICAL), "h $4;", ), ( Gate.Ry(angle=0.17), [4],
def bell_pair(prob): return (Circuit().add_instruction(Instruction( Gate.H(), 0)).add_instruction(Instruction(Gate.CNot(), [0, 1])).add_result_type(prob))
def foo(target): return Circuit().add(Instruction(Gate.H(), 0))