예제 #1
0
def test_deprecated():
    state_vector = np.array([1, 1], dtype=np.complex64) / np.sqrt(2)
    with cirq.testing.assert_logs('state', 'state_vector', 'deprecated'):
        # pylint: disable=unexpected-keyword-arg,no-value-for-parameter
        _ = cirq.bloch_vector_from_state_vector(state=state_vector, index=0)

    with cirq.testing.assert_logs('state', 'state_vector', 'deprecated'):
        # pylint: disable=unexpected-keyword-arg,no-value-for-parameter
        _ = cirq.density_matrix_from_state_vector(state=state_vector)

    with cirq.testing.assert_logs('state', 'state_vector', 'deprecated'):
        # pylint: disable=unexpected-keyword-arg,no-value-for-parameter
        _ = cirq.dirac_notation(state=state_vector)

    with cirq.testing.assert_logs(
        'validate_normalized_state', 'validate_normalized_state_vector', 'deprecated'
    ):
        _ = cirq.validate_normalized_state(state_vector, qid_shape=(2,))

    with cirq.testing.assert_logs('state', 'state_vector', 'deprecated'):
        # pylint: disable=unexpected-keyword-arg,no-value-for-parameter
        _ = cirq.validate_qid_shape(state=state_vector, qid_shape=(2,))
예제 #2
0
    def test_step_by_step_circuit_inspection(self):
        """
        This function demonstrates how to use Cirq to print the state vector of every
        step (moment) in a circuit. It also shows how to get the state vector at each
        step, and how to print it in ket notation.
        """

        qubits = cirq.NamedQubit.range(3, prefix="qubit")
        circuit = cirq.Circuit()
        circuit.append(cirq.H.on_each(*qubits))
        circuit.append(cirq.X(qubits[2]))
        circuit.append(cirq.CNOT(qubits[2], qubits[0]))
        circuit.append(cirq.measure_each(*qubits))

        simulator = cirq.Simulator()
        steps = simulator.simulate_moment_steps(
            circuit)  # Step through each moment of the circuit
        for step in steps:
            print(
                step.state_vector()
            )  # Print the entire state vector for all of the qubits in the circuit
            print(cirq.dirac_notation(step.state_vector(
            )))  # Print the state vector in big-endian ket (Dirac) notation
            print("")
예제 #3
0
def assert_dirac_notation_python(vec, expected, decimals=2):
    assert cirq.dirac_notation(vec, decimals=decimals) == expected
예제 #4
0
def assert_dirac_notation_numpy(vec, expected, decimals=2):
    assert cirq.dirac_notation(np.array(vec), decimals=decimals) == expected
예제 #5
0
 def __str__(self) -> str:
     """Return the state vector string representation of the state."""
     return cirq.dirac_notation(self.to_state_vector())
예제 #6
0
            ]
        )
        yield cirq.ops.Moment([cirq.CNOT(self.physical_qubits[0], self.physical_qubits[3])])
        yield cirq.ops.Moment([cirq.CNOT(self.physical_qubits[0], self.physical_qubits[6])])
        yield cirq.ops.Moment(
            [cirq.CCNOT(self.physical_qubits[3], self.physical_qubits[6], self.physical_qubits[0])]
        )


if __name__ == '__main__':
    # coverage: ignore

    # create circuit with 9 physical qubits
    code = OneQubitShorsCode()

    circuit = cirq.Circuit(code.apply_gate(cirq.X ** (1 / 4), 0))
    print(cirq.dirac_notation(circuit.final_state_vector(initial_state=0)))

    circuit += cirq.Circuit(code.encode())
    print(cirq.dirac_notation(circuit.final_state_vector(initial_state=0)))

    # create error
    circuit += cirq.Circuit(
        code.apply_gate(cirq.X, random.randint(0, code.num_physical_qubits - 1))
    )
    print(cirq.dirac_notation(circuit.final_state_vector(initial_state=0)))

    # correct error and decode
    circuit += cirq.Circuit(code.correct())
    print(cirq.dirac_notation(circuit.final_state_vector(initial_state=0)))
 def __str__(self):
     """Return the wavefunction string representation of the state."""
     return cirq.dirac_notation(self.to_state_vector())