def test_sub(): sub_expr = FreeParameter("theta") - FreeParameter("alpha") expected = FreeParameterExpression( FreeParameter("theta")) - FreeParameterExpression( FreeParameter("alpha")) assert sub_expr == expected
def test_commutativity(): add_1 = 1 + FreeParameterExpression(FreeParameter("theta")) add_2 = FreeParameterExpression(FreeParameter("theta")) + 1 mul_1 = FreeParameterExpression(FreeParameter("theta") * 1) mul_2 = FreeParameterExpression(1 * FreeParameter("theta")) assert add_1 == add_2 assert mul_1 == mul_2
def test_equality(): expr_1 = FreeParameterExpression(FreeParameter("theta") + 1) expr_2 = FreeParameterExpression(FreeParameter("theta") + 1) other_expr = FreeParameterExpression(FreeParameter("theta")) non_expr = "non circuit" assert expr_1 == expr_2 assert expr_1 is not expr_2 assert expr_1 != other_expr assert expr_1 != non_expr
def test_free_param_equality(): param1 = FreeParameter("theta") param2 = FreeParameter("phi") rx1 = Gate.Rx(param1) rx2 = Gate.Rx(param1) other_gate = Gate.Rx(param2) assert rx1 == rx2 assert rx1 is not rx2 assert rx1 != other_gate assert rx1 != param1
def test_symbolic_equality(): symbol1 = FreeParameter("theta") symbol2 = FreeParameter("phi") symbol3 = FreeParameter("theta") gate1 = AngledGate(angle=symbol1, qubit_count=1, ascii_symbols=["bar"]) gate2 = AngledGate(angle=symbol1, qubit_count=1, ascii_symbols=["bar"]) gate3 = AngledGate(angle=symbol3, qubit_count=1, ascii_symbols=["bar"]) other_gate = AngledGate(angle=symbol2, qubit_count=1, ascii_symbols=["foo"]) assert gate1 == gate2 assert gate1 == gate3 assert gate1 is not gate2 assert gate1 != other_gate
def test_angled_gate_with_expr(): expr = FreeParameterExpression(FreeParameter("theta") + 1) new_expr = expr.subs({"theta": 1}) gate = AngledGate(angle=new_expr, qubit_count=1, ascii_symbols=["bar"]) expected = AngledGate(angle=2, qubit_count=1, ascii_symbols=["bar"]) assert gate == expected
def test_adjoint_parameterized(): theta = FreeParameter("theta") angled_gate = AngledGate(angle=(theta + 1), qubit_count=1, ascii_symbols=["foo"]) assert angled_gate.adjoint() == [ AngledGate(angle=-(theta + 1), qubit_count=1, ascii_symbols=["foo"]) ]
def test_noise_multi_probabilities_with_parameter(): a = FreeParameter("a") b = FreeParameter("b") c = FreeParameter("c") circ = Circuit().h(0).x(1).pauli_channel(1, a, b, c) expected = ( "T : | 0 |", " ", "q0 : -H-----------", " ", "q1 : -X-PC(a,b,c)-", "", "T : | 0 |", "", "Unassigned parameters: [a, b, c].", ) _assert_correct_diagram(circ, expected)
def test_bind_values(gate): theta = FreeParameter("theta") param_gate = gate(theta) new_gate = param_gate.bind_values(theta=1) expected = gate(1) assert (type(new_gate.angle) == float and type(new_gate) == type(param_gate) and new_gate == expected)
def test_run_param_circuit(parameterized_quantum_task, device, s3_destination_folder): theta = FreeParameter("theta") circ = Circuit().ry(angle=theta, target=0) _run_and_assert( parameterized_quantum_task, device, circ, s3_destination_folder, shots=10, )
def parameterized_quantum_task(aws_session, s3_destination_folder): theta = FreeParameter("theta") circ = Circuit().ry(angle=theta, target=0) return AwsQuantumTask.create( device_arn="arn:aws:braket:::device/quantum-simulator/amazon/sv1", aws_session=aws_session, poll_timeout_seconds=2, task_specification=circ, shots=10, s3_destination_folder=s3_destination_folder, )
def parameterized_quantum_task_batch(aws_session, s3_destination_folder): theta = FreeParameter("theta") circ_1 = Circuit().ry(angle=3, target=0) circ_2 = Circuit().ry(angle=theta, target=0) return AwsQuantumTaskBatch( device_arn="arn:aws:braket:::device/quantum-simulator/amazon/sv1", aws_session=aws_session, poll_timeout_seconds=2, task_specifications=[circ_1, circ_2], shots=1, s3_destination_folder=s3_destination_folder, max_parallel=100, )
def test_one_gate_one_qubit_rotation_with_parameter_assigned(): theta = FreeParameter("theta") circ = Circuit().rx(angle=theta, target=0) new_circ = circ.make_bound_circuit({"theta": np.pi}) # Column formats to length of the gate plus the ascii representation for the angle. expected = ( "T : | 0 |", " ", "q0 : -Rx(3.14)-", "", "T : | 0 |", ) _assert_correct_diagram(new_circ, expected)
def test_one_gate_one_qubit_rotation_with_unicode(): theta = FreeParameter("\u03B8") circ = Circuit().rx(angle=theta, target=0) # Column formats to length of the gate plus the ascii representation for the angle. expected = ( "T : | 0 |", " ", "q0 : -Rx(θ)-", "", "T : | 0 |", "", "Unassigned parameters: [θ].", ) _assert_correct_diagram(circ, expected)
def test_run_batch_param_circuit(parameterized_quantum_task_batch, aws_session, device, s3_destination_folder): theta = FreeParameter("theta") circ_1 = Circuit().ry(angle=3, target=0) circ_2 = Circuit().ry(angle=theta, target=0) circuits = [circ_1, circ_2] _run_batch_and_assert( parameterized_quantum_task_batch, aws_session, device, circuits, s3_destination_folder, shots=10, )
def test_sub_string(): theta = FreeParameter("theta") expr = theta + 1 assert expr.subs({"theta": 1}) == 2
def test_pow_constant(): mul_expr = FreeParameter("theta")**2 expected = FreeParameterExpression(FreeParameter("theta")**2) assert mul_expr == expected
def test_r_pow(): mul_expr = 2**FreeParameter("theta") expected = FreeParameterExpression(2**FreeParameter("theta")) assert mul_expr == expected
def test_r_sub(): r_sub_expr = 1 - FreeParameter("theta") expected = FreeParameterExpression(1 - FreeParameter("theta")) assert r_sub_expr == expected
def test_pow(): mul_expr = FreeParameter("theta")**FreeParameter("alpha") * 2 expected = FreeParameterExpression( FreeParameter("theta")**FreeParameter("alpha") * 2) assert mul_expr == expected
def test_add(): add_expr = FreeParameter("theta") + FreeParameter("theta") expected = FreeParameterExpression(2 * FreeParameter("theta")) assert add_expr == expected
def test_bind_values(): theta = FreeParameter("theta") gate = AngledGate(angle=theta, qubit_count=1, ascii_symbols=["bar"]) gate.bind_values(theta=1)
def test_mixed_angle_equality(): symbol1 = FreeParameter("theta") gate1 = AngledGate(angle=symbol1, qubit_count=1, ascii_symbols=["bar"]) gate2 = AngledGate(angle=0.15, qubit_count=1, ascii_symbols=["foo"]) assert gate1 != gate2
def test_neg(): expr = FreeParameter("theta") * FreeParameter("alpha") * 2 expected_expr = -FreeParameter("theta") * -FreeParameter("alpha") * -2 assert -expr == expected_expr and -(-expr) == expr
def free_parameter_expression(): return FreeParameterExpression(FreeParameter("theta") - 1)
def test_sub_return_expression(): expr = FreeParameter("theta") + 1 + FreeParameter("alpha") subbed_expr = expr.subs({"alpha": 1}) expected = FreeParameter("theta") + 2 assert subbed_expr == expected
def test_sub_free_parameter(): theta = FreeParameter("theta") expr = theta + 1 param_values = {theta: 1} assert expr.subs(param_values) == 2