# Buggy code
def compose_u3(theta1, phi1, lambda1, theta2, phi2, lambda2):
    """Return a triple theta, phi, lambda for the product.

    u3(theta, phi, lambda)
       = u3(theta1, phi1, lambda1).u3(theta2, phi2, lambda2)
       = Rz(phi1).Ry(theta1).Rz(lambda1+phi2).Ry(theta2).Rz(lambda2)
       = Rz(phi1).Rz(phi').Ry(theta').Rz(lambda').Rz(lambda2)
       = u3(theta', phi1 + phi', lambda2 + lambda')

    Return theta, phi, lambda.
    """
    # Careful with the factor of two in yzy_to_zyz
    thetap, phip, lambdap = yzy_to_zyz((lambda1 + phi2) / 2, theta1 / 2, theta2 / 2)
    (theta, phi, lamb) = (2 * thetap, phi1 + 2 * phip, lambda2 + 2 * lambdap)
    return (theta.simplify(), phi.simplify(), lamb.simplify())


# Traceback (AssertionError)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-9-93ef2321f491> in <module>()
30 backend_coupling = backend_config['coupling_map']
31
---> 32 compiled_circuit = qp.compile([circuitName], backend=backend, coupling_map=backend_coupling)
33 compiled_qasm = qp.get_compiled_qasm(compiled_circuit, circuitName)
34 print("Number of gates at ", circuitName, " circuit is ", len(compiled_qasm.split("\n")) - 4)

~/miniconda3/envs/QISKitenv/lib/python3.6/site-packages/qiskit/_quantumprogram.py in compile(self, name_of_circuits, backend, config, basis_gates, coupling_map, initial_layout, shots, max_credits, seed, qobj_id, hpc, skip_translation)
972                                       config, basis_gates, coupling_map, initial_layout,
973                                       shots, max_credits, seed, qobj_id, hpc,
--> 974                                       skip_translation)
975         return qobj
976

~/miniconda3/envs/QISKitenv/lib/python3.6/site-packages/qiskit/wrapper/_wrapper.py in compile(circuits, backend, config, basis_gates, coupling_map, initial_layout, shots, max_credits, seed, qobj_id, hpc, skip_translation)
171                                     config, basis_gates, coupling_map, initial_layout,
172                                     shots, max_credits, seed, qobj_id, hpc,
--> 173                                     skip_translation)
174
175

~/miniconda3/envs/QISKitenv/lib/python3.6/site-packages/qiskit/_compiler.py in compile(circuits, backend, config, basis_gates, coupling_map, initial_layout, shots, max_credits, seed, qobj_id, hpc, skip_translation)
146                 coupling_map=coupling_map,
147                 initial_layout=initial_layout,
--> 148                 get_layout=True)
149             # Map the layout to a format that can be json encoded
150             list_layout = None

~/miniconda3/envs/QISKitenv/lib/python3.6/site-packages/qiskit/_compiler.py in compile_circuit(quantum_circuit, basis_gates, coupling_map, initial_layout, get_layout, format, seed)
237         cx_cancellation(compiled_dag_circuit)
238         # Simplify single qubit gates
--> 239         compiled_dag_circuit = optimize_1q_gates(compiled_dag_circuit)
240         logger.info("post-mapping properties: %s",
241                     compiled_dag_circuit.property_summary())

~/miniconda3/envs/QISKitenv/lib/python3.6/site-packages/qiskit/mapper/_mapping.py in optimize_1q_gates(circuit)
849                                               right_parameters[0],
850                                               right_parameters[1],
--> 851                                               right_parameters[2])
852                 # Why evalf()? This program:
853                 #   OPENQASM 2.0;

~/miniconda3/envs/QISKitenv/lib/python3.6/site-packages/qiskit/mapper/_mapping.py in compose_u3(theta1, phi1, lambda1, theta2, phi2, lambda2)
736     # Careful with the factor of two in yzy_to_zyz
737     thetap, phip, lambdap = yzy_to_zyz((lambda1 + phi2) / 2,
--> 738                                        theta1 / 2, theta2 / 2)
739     (theta, phi, lamb) = (2 * thetap, phi1 + 2 * phip, lambda2 + 2 * lambdap)
740     return (theta.simplify(), phi.simplify(), lamb.simplify())

~/miniconda3/envs/QISKitenv/lib/python3.6/site-packages/qiskit/mapper/_mapping.py in yzy_to_zyz(xi, theta1, theta2, eps)
720     logger.debug("solutions=%s", pprint.pformat(solutions))
721     logger.debug("deltas=%s", pprint.pformat(deltas))
--> 722     assert False, "Error! No solution found. This should not happen."
723
724

AssertionError: Error! No solution found. This should not happen.

# Fixed code
def compose_u3(theta1, phi1, lambda1, theta2, phi2, lambda2):
    """Return a triple theta, phi, lambda for the product.

    u3(theta, phi, lambda)
       = u3(theta1, phi1, lambda1).u3(theta2, phi2, lambda2)
       = Rz(phi1).Ry(theta1).Rz(lambda1+phi2).Ry(theta2).Rz(lambda2)
       = Rz(phi1).Rz(phi').Ry(theta').Rz(lambda').Rz(lambda2)
       = u3(theta', phi1 + phi', lambda2 + lambda')

    Return theta, phi, lambda.
    """
    # Careful with the factor of two in yzy_to_zyz
    thetap, phip, lambdap = yzy_to_zyz((lambda1 + phi2), theta1, theta2)
    (theta, phi, lamb) = (thetap, phi1 + phip, lambda2 + lambdap)
    return (theta, phi, lamb)