コード例 #1
0
ファイル: sample_test.py プロジェクト: xj361685640/quantum-1
 def test_sample_create(self):
     """Test that sample instantiates correctly."""
     sample.Sample(backend=cirq.Simulator())
     sample.Sample()
     with self.assertRaisesRegex(TypeError,
                                 expected_regex="junk is invalid"):
         sample.Sample(backend='junk')
コード例 #2
0
ファイル: sample_test.py プロジェクト: xj361685640/quantum-1
    def test_sample_invalid_shape_inputs(self):
        """Test that sample rejects bad input shapes."""
        sampler = sample.Sample()
        with self.assertRaisesRegex(TypeError,
                                    expected_regex="string or sympy.Symbol"):
            sampler(cirq.Circuit(),
                    symbol_values=[[0.5]],
                    symbol_names=[[]],
                    repetitions=10)

        with self.assertRaisesRegex(ValueError,
                                    expected_regex="rank 2 but is rank 1"):
            sampler(cirq.Circuit(),
                    symbol_values=[0.5],
                    symbol_names=['name'],
                    repetitions=10)

        with self.assertRaisesRegex(ValueError,
                                    expected_regex="rank 1 but is rank 2"):
            sampler([[cirq.Circuit()]],
                    symbol_values=[[0.5]],
                    symbol_names=['name'],
                    repetitions=10)

        with self.assertRaisesRegex(
                TypeError, expected_regex="cannot be parsed to int32 tensor"):
            sampler([cirq.Circuit()], repetitions=[10])
コード例 #3
0
 def test_sample_invalid_type_inputs(self):
     """Test that sample rejects bad inputs."""
     sampler = sample.Sample()
     with self.assertRaisesRegex(
             ValueError, expected_regex="repetitions not specified"):
         sampler(cirq.Circuit())
     with self.assertRaisesRegex(ValueError,
                                 expected_regex="greater than zero"):
         sampler(cirq.Circuit(), repetitions=-1)
     with self.assertRaisesRegex(
             TypeError, expected_regex="cannot be parsed to int32"):
         sampler(cirq.Circuit(), repetitions='junk')
コード例 #4
0
ファイル: sample_test.py プロジェクト: xj361685640/quantum-1
 def test_sample_basic_inputs(self):
     """Test that sample ingests inputs correctly in simple settings."""
     sampler = sample.Sample()
     sampler(cirq.Circuit(), repetitions=10)
     sampler([cirq.Circuit()], repetitions=10)
     sampler(cirq.Circuit(),
             symbol_names=['name'],
             symbol_values=[[0.5]],
             repetitions=10)
     sampler(cirq.Circuit(),
             symbol_names=[sympy.Symbol('name')],
             symbol_values=[[0.5]],
             repetitions=10)
コード例 #5
0
ファイル: sample_test.py プロジェクト: xj361685640/quantum-1
    def test_sample_invalid_type_inputs(self):
        """Test that sample rejects bad inputs."""
        sampler = sample.Sample()
        with self.assertRaisesRegex(
                TypeError, expected_regex="circuits cannot be parsed"):
            sampler('junk_circuit', repetitions=10)

        with self.assertRaisesRegex(
                TypeError, expected_regex="symbol_values cannot be parsed"):
            sampler(cirq.Circuit(), symbol_values='junk', repetitions=10)

        with self.assertRaisesRegex(
                TypeError, expected_regex="symbol_names cannot be parsed"):
            sampler(cirq.Circuit(),
                    symbol_values=[],
                    symbol_names='junk',
                    repetitions=10)

        with self.assertRaisesRegex(TypeError,
                                    expected_regex="Cannot convert"):
            sampler(cirq.Circuit(),
                    symbol_values=[['bad']],
                    symbol_names=['name'],
                    repetitions=10)

        with self.assertRaisesRegex(TypeError,
                                    expected_regex="must be a string."):
            sampler(cirq.Circuit(),
                    symbol_values=[[0.5]],
                    symbol_names=[0.33333],
                    repetitions=10)

        with self.assertRaisesRegex(ValueError,
                                    expected_regex="must be unique."):
            sampler(cirq.Circuit(),
                    symbol_values=[[0.5]],
                    symbol_names=['duplicate', 'duplicate'],
                    repetitions=10)

        with self.assertRaisesRegex(
                ValueError, expected_regex="repetitions not specified"):
            sampler(cirq.Circuit())

        with self.assertRaisesRegex(ValueError,
                                    expected_regex="greater than zero"):
            sampler(cirq.Circuit(), repetitions=-1)

        with self.assertRaisesRegex(
                TypeError, expected_regex="cannot be parsed to int32"):
            sampler(cirq.Circuit(), repetitions='junk')
コード例 #6
0
    def test_sample_invalid_combinations(self, backend):
        """Test with valid type inputs and valid value, but incorrect combo."""
        sampler = sample.Sample(backend)
        symbol = sympy.Symbol('alpha')
        circuit = cirq.Circuit(cirq.H(cirq.GridQubit(0, 0))**symbol)
        with self.assertRaisesRegex(Exception, expected_regex=""):
            # no value provided.
            sampler([circuit, circuit], symbol_names=[symbol], repetitions=5)

        with self.assertRaisesRegex(Exception, expected_regex=""):
            # no name provided.
            sampler([circuit, circuit],
                    symbol_names=[],
                    symbol_values=[[2.0], [3.0]],
                    repetitions=5)

        with self.assertRaisesRegex(Exception, expected_regex=""):
            # deceptive, but the circuit shouldn't be in a list. otherwise fine.
            sampler([circuit],
                    symbol_names=['alpha'],
                    symbol_values=[[2.0], [3.0]],
                    repetitions=5)

        with self.assertRaisesRegex(Exception, expected_regex=""):
            # wrong symbol name.
            sampler([circuit],
                    symbol_names=['alphaaaa'],
                    symbol_values=[[2.0], [3.0]],
                    repetitions=5)

        with self.assertRaisesRegex(Exception, expected_regex=""):
            # too many symbol values provided.
            sampler(circuit,
                    symbol_names=['alpha'],
                    symbol_values=[[2.0, 4.0], [3.0, 5.0]],
                    repetitions=5)

        with self.assertRaisesRegex(Exception, expected_regex=""):
            # Wrong batch_size for symbol values.
            sampler([circuit],
                    symbol_names=['alpha'],
                    symbol_values=np.zeros((3, 1)),
                    repetitions=5)
コード例 #7
0
    def test_sample_output(self, backend, all_n_qubits, n_samples,
                           symbol_names):
        """Test that expected output format is preserved.

        Check that any pre or post processing done inside the layers does not
        cause what is output from the layer to structurally deviate from what
        is expected.
        """
        sampler = sample.Sample(backend=backend)
        bits = cirq.GridQubit.rect(1, max(all_n_qubits))
        programs = []
        expected_outputs = []
        for n_qubits in all_n_qubits:
            programs.append(cirq.Circuit(*cirq.X.on_each(*bits[0:n_qubits])))
            expected_outputs.append([[1] * n_qubits for _ in range(n_samples)])
        symbol_values = np.random.random((len(all_n_qubits), len(symbol_names)))
        layer_output = sampler(programs,
                               symbol_names=symbol_names,
                               symbol_values=symbol_values,
                               repetitions=n_samples).to_list()
        self.assertEqual(expected_outputs, layer_output)
コード例 #8
0
ファイル: sample_test.py プロジェクト: xj361685640/quantum-1
 def test_sample_outputs_simple(self):
     """Test the simplest call where nothing but circuits are provided."""
     sampler = sample.Sample()
     circuit = cirq.Circuit(cirq.H(cirq.GridQubit(0, 0)))
     output = sampler([circuit, circuit], repetitions=5)
     self.assertShapeEqual(np.empty((2, 5, 1)), output.to_tensor())